Back to Lessons

Exception Handling in Java

April 5, 2026

Exception Handling

Gracefully handle runtime errors without program crash.

Try-Catch Example

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
} catch (Exception e) {
    System.out.println("General error");
} finally {
    System.out.println("Cleanup code");
}

Key Points

  • Hierarchy: Throwable > Exception > RuntimeException.
  • throws in method signature.
  • throw manually throws exception.
  • Finally block always executes.