Checked vs. Unchecked Exceptions

Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

Joshua Bloch, Effective Java
This might be a little abstract, so we to concretize to
User’s error vs. Programmer’s error.

RuntimeException and all its child classes indicate a programmer’s error, while Exception and all its child classes indicate a user’s error:

  1. FileNotFoundException: Can happen anytime, its not a programmer’s mistake but a user’s error (choose a wrong file, working with a read-only or full file system, etc.)
  2. SQLException: Something went wrong with the database operation. Try to recover or notify the user. This example fits very good to the above mentioned ‘recoverable’ condition but can be as well caused by a user’s error, e.g. an invalid search query.
  3. NullPointerException: Clearly a programmer’s error, don’t catch it but make it not happen.
  4. ArithmeticException: Happens for example when you divide by zero. A programmer’s error. Don’t divide by zero.
  5. UnsupportedOperationException: Thrown for example by immutable implementations of Collection (e.g. Collections.unmodifiableList()). It’s the responsibility of the programmer to handle this case.
Share itShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *