Checked vs Unchecked exceptions

Exceptions in Java can be broadly categorized into checked and unchecked exceptions. A checked exception is one for which compiler will force you to handle it (either catch it, or throw it back), whereas for an unchecked exception you are not forced to provide handler. So when you are creating a new exception, you can decide whether it is a checked (extending a checked exception) or unchecked exception (extending unchecked exception).

Now the challenge is, how to decide if an excpetion should be checked or unchecked. That is tricky.

“Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.”

And to sum it up

“Here’s the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”

Text Source: http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html