Java Exception Handling
Introduction to Exception Handling
Exception handling in Java is a mechanism to handle runtime errors, maintaining the normal flow of the application. An exception is an event that disrupts the normal flow of the program.
Java exceptions are categorized into two types:
1. Checked exceptions - compile-time exceptions that must be handled
2. Unchecked exceptions - runtime exceptions that don't need to be explicitly handled
Exception Hierarchy
Exception Type | Description | Examples |
---|---|---|
Checked Exceptions | Exceptions checked at compile time | IOException, SQLException, ClassNotFoundException |
Unchecked Exceptions | Exceptions not checked at compile time | NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException |
Errors | Serious problems that applications should not try to catch | OutOfMemoryError, StackOverflowError, VirtualMachineError |
try-catch-finally Blocks
The try block contains code that might throw an exception. The catch block handles the exception. The finally block always executes, whether an exception occurred or not.
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println("Accessing element at index 5: " + numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("This block always executes");
}
System.out.println("Program continues after exception handling");
}
}
Exception caught: Index 5 out of bounds for length 3 This block always executes Program continues after exception handling
Multiple catch Blocks
You can have multiple catch blocks to handle different types of exceptions. The catch blocks are evaluated in order, so more specific exceptions should come before more general ones.
public class MultipleCatchExample {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b;
String str = null;
System.out.println(str.length());
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: " + e.getMessage());
} catch (NullPointerException e) {
System.out.println("NullPointerException caught: " + e.getMessage());
} catch (Exception e) {
System.out.println("General Exception caught: " + e.getMessage());
}
}
}
ArithmeticException caught: / by zero
throw and throws Keywords
The throw keyword is used to explicitly throw an exception. The throws keyword is used in method signatures to declare that a method might throw exceptions.
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class ThrowThrowsExample {
public static void validateAge(int age) throws CustomException {
if (age < 18) {
throw new CustomException("Age must be 18 or older");
} else {
System.out.println("Age is valid");
}
}
public static void main(String[] args) {
try {
validateAge(15);
} catch (CustomException e) {
System.out.println("Caught custom exception: " + e.getMessage());
}
try {
validateAge(20);
} catch (CustomException e) {
System.out.println("Caught custom exception: " + e.getMessage());
}
}
}
Caught custom exception: Age must be 18 or older Age is valid
try-with-resources
The try-with-resources statement automatically closes resources that implement the AutoCloseable interface. This ensures proper resource management and avoids resource leaks.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TryWithResourcesExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("IOException caught: " + e.getMessage());
}
}
}
Best Practices
- ✅ Catch specific exceptions rather than using generic Exception catching
- ✅ Use finally blocks to clean up resources (or use try-with-resources)
- ✅ Don't swallow exceptions - at least log them
- ✅ Create custom exceptions for business logic errors
- ✅ Use exception chaining to preserve the original exception
- ✅ Avoid using exceptions for control flow - they're expensive operations