Item 58 - Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

From Effective Java 2/e by Joshua Bloch

The Java programming language provides three kinds of throwables:

  1. checked exceptions
    • use checked exceptions for conditions from which the caller can reasonably be expected to recover
  2. runtime exceptions (unchecked exceptions)
    • Use runtime exceptions to indicate programming errors
    • all of the unchecked throwables you implement should subclass RuntimeException
  3. errors

Checked vs Unchecked Exceptions

  • Checked exceptions must be explicitly caught or propagated as described in Basic try-catch-finally Exception Handling.
  • Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown.
  • Checked exceptions in Java extend the java.lang.Exception class.
  • Unchecked exceptions extend the java.lang.RuntimeException.

Posted by The Finest Artist