Reputation:
I am curious about if I should add throws ExceptionClass
or not after the method's signature.(ExceptionClass extends RuntimeException)
For instance:
public void foo() // throws ExceptionClass
{
// ...
throw new ExceptionClass("");
}
Upvotes: 4
Views: 6082
Reputation: 13151
Ideally you don't need to add runtime exception in method's throws clause. Since you want the consumer of this method to be aware of chances that this method may throw exception , i would say use javadoc either. Below is example of how you should use :
/**
*
* @throws ExceptionClass
*/
public void foo()
{
// ...
throw new ExceptionClass("");
}
Upvotes: 2
Reputation: 328619
If you use the JDK as a guidance, RuntimeException are not in the methods signature but are documented in the javadoc. You can have a look at collections for example.
Upvotes: 9
Reputation: 10762
Runtime exceptions do not need to be listed in the throws
clause. Place it there only if you need to document this behaviour, if it's in some way important to know for the client of this class, that this exception might appear (but if that's the case, perhaps it needs to be changed to a checked exception, or only be mentioned in the javadocs).
Upvotes: 0