If I have a method which throws an unchecked exception, e.g.:
void doSomething(int i) {
if (i < 0) throw new IllegalArgumentException("Too small");
// ...
}
is there any advantage to explicitly declaring that the method throws the exception, i.e.
void doSomething(int i) throws IllegalArgumentException {
if (i < 0) throw new IllegalArgumentException("Too small");
// ...
}
as opposed to (or in addition to) describing the behaviour in javadoc:
/**
* This method does something useful.
* @param i some input value
* @throws IllegalArgumentException if {@code i < 0}
*/
void doSomething(int i) {
if (i < 0) throw new IllegalArgumentException("Too small");
// ...
}
The reasons why I would claim it is not useful to have the throws
are:
throws
provides no information as to the circumstances under which the exception will be thrown, only that it might be thrown;doSomething
;doSomething
might invoke code which throws other types of unchecked exception; claiming that 'this method throws IllegalArgumentException
' seems like it's only telling part of the story, potentially;The reasons why I would claim it would be useful to have the throws
are:
In short, I think that throws
is unnecessary, but a javadoc description via @throws
is useful. I would be interested to know others' opinion on this.
You can but never need to declare unchecked exceptions, whether you declare them yourself or not. Exceptions inheriting from RuntimeException are unchecked. Save this answer.
An unchecked exception (also known as an runtime exception) in Java is something that has gone wrong with the program and is unrecoverable. Just because this is not a compile time exception, meaning you do not need to handle it, that does not mean you don't need to be concerned about it.
Unchecked exceptions can be propagated in the call stack using the throw keyword in a method. Checked exceptions can be propagated using the throw keyword when the method that throws the exception declares it using the throws keyword.
Since unchecked exceptions indicate programming errors, declaring them in the throws clause should be avoided. Generally, catching these exceptions should not be attempted, except for the highest level of your program.
If the user of your API cannot see your source code he wouldn't see the javadoc comments. That's why declaring the throws
clause could be useful.
Also it is easier for some programmers to quickly determine the exception from method signature than to see what is there inside javadoc.
But in general I think that it's more useful to list unchecked exceptions only in javadocs because if there are both checked and unchecked exceptions in throws
clause the situation could be confusing. You cannot determine the type of exception without compiler or without looking into each exception class signature.
However unchecked exceptions mean that situation is critical and couldn't be fixed by the program at runtime. If you use unchecked exceptions by purpose of checked exceptions (you assume that situation could be fixed) but for some reason you don't want the compiler to force catching the exception then I recommend to put the exception inside throws
clause too.
When you state that a method throws
an exception you are saying to the caller:
You have two choices:
In case 1 it may remind user to implement a finally
- which could be a bonus.
In case 2 it focuses the mind on the exception which could also be a bonus.
If you hide that possibility then neither of the above reminders occur to the user.
To me one may unnecessarily clutter up their code while the other keeps it sweet and simple. However, one encourages focus on potential issues while the other may leave you in blissful ignorance.
Bottom line - Ask yourself how irritating it will be to declare the exception as thrown (e.g. should you declare throws NullPointerException
? - NO!) and is this irritation balanced by the upside of focusing the users mind on catch
, finally
and throws
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With