Multiple catch Blocks
Multiple catch blocks allow a single try block to handle different exception types differently. This enables precise error handling, better diagnostics, and cleaner recovery logic.
This is a very common interview topic, especially around ordering rules and reachability.
Why Multiple catch Blocks Are Needed
- Different exceptions require different handling
- Improves clarity and control
- Avoids catching overly generic exceptions
- Enhances robustness of applications
Basic Syntax
try {
// risky code
} catch (ExceptionType1 e1) {
// handling logic for ExceptionType1
} catch (ExceptionType2 e2) {
// handling logic for ExceptionType2
}
- ✔ Only one matching catch executes
- ✔ Remaining catch blocks are skipped
Simple Example
try {
int[] arr = new int[3];
int result = 10 / 0;
System.out.println(arr[5]);
} catch (ArithmeticException e) {
System.out.println("Arithmetic error");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index error");
}
Output:
Arithmetic error
Result: ✔ First exception stops further execution in try
Catch Block Execution Rules (Very Important)
1. Only One catch Executes
- As soon as an exception occurs
- JVM jumps to the first matching catch
- Remaining try statements are skipped
2. Catch Blocks Are Checked Top to Bottom
- Order matters
- First matching catch is chosen
Catch Ordering Rule (Interview Favorite)
Child exceptions must be caught before parent exceptions.
❌ Invalid (Unreachable Code)
try {
int x = 10 / 0;
} catch (Exception e) {
System.out.println("Exception");
} catch (ArithmeticException e) {
System.out.println("Arithmetic");
}
✔ Compile-time error: ArithmeticException is unreachable
✅ Valid Order
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Arithmetic");
} catch (Exception e) {
System.out.println("Exception");
}
Multiple Catch with Related Exceptions
try {
String s = null;
s.length();
} catch (NullPointerException e) {
System.out.println("Null reference");
} catch (RuntimeException e) {
System.out.println("Runtime issue");
}
- ✔ Specific first
- ✔ General later
Multi-Catch (Java 7+) — Alternative
Handle multiple unrelated exceptions in one catch.
try {
int a = 10 / 0;
} catch (ArithmeticException | NullPointerException e) {
System.out.println("Handled");
}
Rules:
- Exceptions must be unrelated
- Variable e is implicitly final
Multiple Catch vs Multi-Catch
| Aspect | Multiple Catch | Multi-Catch |
|---|---|---|
| Handling logic | Separate per exception | Same logic |
| Readability | High (detailed) | Compact |
| Flexibility | More | Less |
| Java version | All | Java 7+ |
Best Practices
- Catch specific exceptions first
- Avoid catching generic Exception unless necessary
- Keep handling logic meaningful
- Log or rethrow when appropriate
- Avoid empty catch blocks
Common Beginner Mistakes
- Wrong catch order
- Catching Exception too early
- Assuming all catch blocks run
- Overusing generic catch
- Ignoring exception details
Interview-Ready Answers
Short Answer
Multiple catch blocks allow handling different exceptions separately for the same try block.
Detailed Answer
In Java, multiple catch blocks can follow a try block to handle different exception types. The JVM selects the first matching catch block based on the exception thrown, and catch blocks must be ordered from most specific to most general to avoid unreachable code.
Key Takeaway
Multiple catch blocks provide precise control over exception handling. Correct ordering and specificity are critical for safe, readable, and maintainable Java code.