Can you have multiple catch blocks for a single try block?
Yes, Java allows you to have multiple catch blocks for a single try block. This is useful when you want to handle different types of exceptions separately. Each catch block is designed to handle a specific type of exception, and the Java runtime will execute the first catch block that matches the exception type.
How It Works
1. Order of catch Blocks
-
The order of catch blocks matters. The more specific exceptions should come before the more general ones.
-
If a general exception (like Exception) appears before specific ones, the specific blocks will be unreachable, leading to a compile-time error.
2. Control Flow
-
When an exception is thrown, Java checks each catch block in order.
-
It executes the first catch block with a matching exception type.
-
If no matching block is found, the exception propagates up the call stack.
Syntax
try {
// Code that may throw multiple exceptions
} catch (ExceptionType1 e1) {
// Handle ExceptionType1
} catch (ExceptionType2 e2) {
// Handle ExceptionType2
} catch (Exception e) {
// Handle any other exceptions
}
Example
public class MultipleCatchExample {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 20; // Throws ArrayIndexOutOfBoundsException
int result = 10 / 0; // This will not be reached
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage());
} catch (Exception e) {
System.out.println("General exception caught: " + e.getMessage());
}
}
}
Output
ArrayIndexOutOfBoundsException caught: Index 10 out of bounds for length 5
Common Scenarios
1. Specific to General Order
try {
String str = null;
System.out.println(str.length()); // Throws NullPointerException
} catch (NullPointerException e) {
System.out.println("NullPointerException caught.");
} catch (Exception e) {
System.out.println("General exception caught.");
}
NullPointerException caught.
2. Incorrect Order (Compilation Error)
try {
String str = null;
System.out.println(str.length()); // Throws NullPointerException
} catch (Exception e) {
System.out.println("General exception caught.");
} catch (NullPointerException e) { // Unreachable
System.out.println("NullPointerException caught.");
}
exception NullPointerException has already been caught
Using Multi-Catch (Java 7 and Later)
Instead of writing multiple catch blocks, you can catch multiple exceptions in a single catch block using the | (OR) operator. |
Example:
public class MultiCatchExample {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 20; // Throws ArrayIndexOutOfBoundsException
int result = 10 / 0; // Throws ArithmeticException
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}
Exception caught: Index 10 out of bounds for length 5
Key Points
-
You can have multiple catch blocks for different exception types.
-
The order of catch blocks should go from specific exceptions to general ones.
-
Use multi-catch blocks to simplify code when handling multiple related exceptions.
-
The catch block can only handle checked exceptions declared in the try block or unchecked exceptions that may occur.