Exception Hierarchy and Checked vs Unchecked

Java exceptions derive from Throwable. Error generally represents serious JVM/runtime conditions. Exception represents conditions applications may handle; RuntimeException and subclasses are unchecked.

Checked exceptions must be caught or declared. Neither category should be chosen mechanically; decide based on whether the API contract benefits from requiring callers to explicitly handle a recoverable condition.

try {
    Files.readString(Path.of("config.txt"));
} catch (IOException e) {
    throw new ConfigurationException("Cannot read configuration", e);
}
Key Takeaways
  • Checked exceptions are enforced by the compiler.
  • Unchecked exceptions include many invalid-state and programming failures.
  • Preserve the original cause when wrapping exceptions.