Exception Handling in Java
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.
throwsin method signature.throwmanually throws exception.- Finally block always executes.