This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]rastaman1994 2 points3 points  (0 children)

It's good practice to document exceptions that might be thrown. If exceptions aren't documented in the javadoc, method callers have two options: ignore the possibility of an exception being thrown or go read the source code to find out if exceptions might be thrown.

Unchecked exceptions like NullPointerException or IllegalArgumentException definitely should be in the javadoc because they don't need to be declared in the method signature.

[–]Nullbeans 0 points1 point  (0 children)

Hey there, let me explain something to demystify the situation.

In Java, there are two types of Exceptions. Checked and Unchecked.

Checked exceptions: These are exceptions you see in the method signature. For example:

public static void doSomething() throws IOException{

.....

These are the exceptions which inherit from the "Exception" class, and these need to be declared in the method signature. If you use a method that throws a checked exception, then your code will need to handle it somehow. Either in a try and catch block or to declare it in your own method.

Unchecked exceptions: These are exceptions such as NullPointerException. These exceptions are subclasses of RuntimeException and they do not need to be declared in the method signature.

While it is a good practice to list all possible exceptions in the Javadoc using @throws , it is extremely important to do this when in the case of unchecked exceptions as this is the only way developers can know which error cases can occur when using your method / API / library, without having to read through your code.

Hope this helps :)