Wednesday, September 24, 2014

EJ-59 Avoid Unnecessary Use of Checked Exceptions

Ref: Effective Java by Joshua Bloch

Checked exceptions are a wonderful feature of the Java programming language. Unlike return codes, they force  the programmer to deal with exceptional conditions, greatly enhancing reliability. That said, overuse of checked exceptions can make an API far less pleasent to use. It places a nontrivial burden on the programmer.

The burden is justified if the exceptional condition can not be prevented by proper use of the API and the programmer using the API cantake some useful actions once confronted with the exception. Unless both of these conitionshold, an unchecked exception is more appropriate. As litmus test, ask yourself how the programmer will handle the exception, Is this the best that can be done?

How about this?

If the programmer using the API can do no better, an unchecked exception would be more appropriate.

No comments: