Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you document unchecked exceptions? [closed]

Tags:

Joshua Bloch in his Effective Java writes :

"Use the Javadoc @throws tag to document each unchecked exception that a method can throw, but do not use the throws keyword to include unchecked exceptions in the method declaration. "

Well that sounds reasonable indeed, but how to find out, what unchecked exception can my method throw?

Let's think of a following class :

public class FooClass {      private MyClass[] myClass;      /**      * Creates new FooClass      */     public FooClass() {          // code omitted         // do something with myClass     }      /**      * Performs foo operation.<br />      * Whatever is calculated.      * @param index Index of a desired element      * @throws HorribleException When something horrible happens during computation      */     public void foo(int index) {         try {             myClass[index].doComputation();         } catch (MyComputationException e) {             System.out.println("Something horrible happened during computation");             throw new HorribleException(e);         }     } } 

Now, I documented HorribleException, but it is quite obvious, that foo method can also throw unchecked java.lang.ArrayIndexOutOfBoundsException. The more complex the code gets, it's harder to think of all unchecked exceptions that method can throw. My IDE doesn't help me much there and nor does any tool. Since I don't know any tool for this ...

How do you deal with this kind of situation?

like image 228
Xorty Avatar asked Sep 19 '10 18:09

Xorty


People also ask

Should unchecked exceptions be documented?

In general, you shouldn't document al the exceptions that can arise. You can't predict them all, and you're very likely to forget one. So, document the obvious ones, the one you though of. Try to list the most exceptions as possible, but don't spend to much time on it.

How do you handle unchecked exceptions?

A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.

What happens when unchecked exception is thrown?

If a program throws an unchecked exception, it reflects some error inside the program logic. Java does not verify unchecked exceptions at compile-time. Furthermore, we don't have to declare unchecked exceptions in a method with the throws keyword.

What is an example of an unchecked exception?

Unchecked exception example Unchecked exceptions result from faulty logic that can occur anywhere in a software program. For example, if a developer invokes a method on a null object, an unchecked NullPointerException occurs.


1 Answers

Only document those which you're explicitly throwing yourself or are passing through from another method. The remnant is to be accounted as bugs which are to be fixed by good unit testing and writing solid code.

In this particular case, I'd account ArrayIndexOutOfBoundsException as a bug in your code and I'd fix the code accordingly that it never throws it. I.e. add a check if the array index is in range and handle accordingly by either throwing an exception -which you document- or taking an alternative path.

like image 192
BalusC Avatar answered Sep 18 '22 15:09

BalusC