Multiple catch Blocks in Java – Explained with Examples
One block of code can fail in several different ways. Reading a value from an array and dividing it might fail because the index is wrong, the text is not a number, or the divisor is zero. Multiple catch blocks let you handle each kind of failure differently, and Java 7's multi-catch lets you handle several kinds the same way in one block.
This tutorial explains how Java chooses a catch block, why their order matters, how to use multi-catch, and the rules the compiler enforces.
1. Syntax
try {
// risky code
} catch (ExceptionTypeA e) {
// handle A
} catch (ExceptionTypeB e) {
// handle B
} catch (ExceptionTypeC | ExceptionTypeD e) { // multi-catch (Java 7+)
// handle C and D the same way
}
2. Handling Each Exception Differently
public class MultipleCatch {
static void process(String[] data, int index, int divisor) {
try {
int value = Integer.parseInt(data[index]);
System.out.println(data[index] + " / " + divisor + " = " + value / divisor);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("No item at index " + index);
} catch (NumberFormatException e) {
System.out.println("'" + data[index] + "' is not a number");
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
public static void main(String[] args) {
String[] data = {"100", "abc", "60"};
process(data, 0, 4);
process(data, 5, 4);
process(data, 1, 4);
process(data, 2, 0);
}
}
100 / 4 = 25 No item at index 5 'abc' is not a number Cannot divide by zero
Only one catch block runs per exception: Java checks the catch blocks from top to bottom and runs the first one whose type matches. The others are skipped.
3. Why the Order of catch Blocks Matters
A catch block also matches all subclasses of its type. If a general type comes first, it catches everything and the specific blocks below can never run. So always order catch blocks from most specific to most general:
public class OrderMatters {
public static void main(String[] args) {
Object[] samples = {null, "text", 42};
for (Object s : samples) {
try {
Integer n = (Integer) s;
System.out.println("number: " + (n * 2));
} catch (NullPointerException e) { // specific types first
System.out.println("null value");
} catch (ClassCastException e) {
System.out.println("not a number: " + s);
} catch (RuntimeException e) { // general fallback last
System.out.println("other problem: " + e);
}
}
}
}
null value not a number: text number: 84
Writing catch (RuntimeException e) above catch (ClassCastException e) would not compile, because the second block would be unreachable ("exception ClassCastException has already been caught").
4. Multi-catch (Java 7+)
When several exception types need the same handling, list them in one catch block separated by |. This removes duplicated code:
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
public class MultiCatch {
static void parseOrder(String line) {
try {
String[] p = line.split(";");
int qty = Integer.parseInt(p[0]);
LocalDate date = LocalDate.parse(p[1]);
System.out.println("ok: " + qty + " on " + date);
} catch (NumberFormatException | DateTimeParseException e) { // same handling for both
System.out.println("bad data in '" + line + "': " + e.getClass().getSimpleName());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("missing field in '" + line + "'");
}
}
public static void main(String[] args) {
parseOrder("3;2026-10-11");
parseOrder("three;2026-10-11");
parseOrder("3;2026-02-30");
parseOrder("3");
}
}
ok: 3 on 2026-10-11 bad data in 'three;2026-10-11': NumberFormatException bad data in '3;2026-02-30': DateTimeParseException missing field in '3'
Multi-catch rules
- The types in a multi-catch must not be related by inheritance. If one is a subclass of another, just catch the parent:
Bad.java:3: error: Alternatives in a multi-catch statement cannot be related by subclassing
catch (NumberFormatException | IllegalArgumentException e) { }
^
Alternative NumberFormatException is a subclass of alternative IllegalArgumentException
- The catch variable is implicitly
final; you cannot assign a new value to it. - Inside the block, the variable's type is the closest common supertype, so you can only call methods available on it (for example
getMessage()). - Rethrowing it with
throw e;is allowed, and the compiler still knows the precise types that can be thrown.
5. Separate catch Blocks or Multi-catch?
| Use separate catch blocks when… | Use multi-catch when… |
|---|---|
| Each exception needs a different response | Several exceptions need exactly the same response |
| Exceptions are related by inheritance (order them) | The exceptions are unrelated types |
| You need the specific type's methods | Common methods like getMessage() are enough |
6. Tips
- Do not catch exceptions you cannot handle meaningfully; let them propagate.
- A final
catch (Exception e)as a fallback is acceptable at the top level of an application, but log it. - Keep each catch block short; move complex recovery into methods.
- Combine with
finallyor try-with-resources for clean-up that must always happen.
7. Summary
- A try block can be followed by several catch blocks for different exception types.
- Java runs only the first catch whose type matches, checking from top to bottom.
- Order catch blocks from specific to general; otherwise the compiler reports unreachable code.
- Multi-catch (
catch (A | B e)) handles unrelated exception types in one block. - The multi-catch variable is final, and the types cannot be subclasses of one another.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. Can a try block have multiple catch blocks?
Yes. Each catch block handles a different exception type, and Java runs the first one that matches the thrown exception.
Q2. Can more than one catch block run for one exception?
No. Exactly one catch block (or none) runs for each exception thrown from the try block.
Q3. In what order should catch blocks be written?
From the most specific subclass to the most general superclass. A superclass catch placed first would make subclass catches unreachable, which is a compile error.
Q4. What is multi-catch in Java?
A Java 7 feature that lets one catch block handle several exception types: catch (IOException | SQLException e).
Q5. Why can't multi-catch types be related?
If one type is a subclass of the other, the parent already covers the child, so listing both is redundant. The compiler rejects it to keep the code clear.
Q6. Is the variable in a multi-catch final?
Yes, implicitly. You cannot assign a new value to it, although you can call its methods and rethrow it.
Q7. What is the type of the variable in a multi-catch?
Effectively the closest common supertype of the listed exceptions, so only methods of that supertype can be called without a cast.
Q8. What error do I get for catching a subclass after its superclass?
"exception X has already been caught", because the earlier superclass catch would always handle X first.
Q9. Can I catch both checked and unchecked exceptions in multi-catch?
Yes, as long as they are not related by inheritance, for example catch (IOException | IllegalStateException e).
Q10. Should I add catch (Exception e) at the end?
Only where catching everything is intended, such as the top level of a request handler that logs unexpected errors. Elsewhere it can hide bugs.
Q11. Does the order matter for unrelated exception types?
No. For types that are not in the same inheritance line, their relative order does not affect which block runs.
Q12. Can I use multiple catch blocks with try-with-resources?
Yes. A try-with-resources statement can have any number of catch blocks and a finally block, like a normal try.
Q13. How do I find out which exception occurred in a multi-catch?
Use e.getClass() or instanceof checks inside the block. If you need very different handling, separate catch blocks are clearer.
Q14. Is multi-catch faster than separate catch blocks?
The performance difference is negligible. Its benefit is less duplicated code and smaller bytecode.
Q15. Can a catch block catch an exception thrown by another catch block?
No. Catch blocks of the same try only handle exceptions from the try block. An exception thrown in a catch block goes to an enclosing try or leaves the method.
Q16. What happens if no catch block matches?
The finally block (if any) runs and the exception propagates to the caller.
Q17. Can I rethrow the exception from a multi-catch?
Yes, with throw e;. Thanks to precise rethrow analysis, the method only needs to declare the actual checked types listed, not their common supertype.
Q18. How many catch blocks can a try have?
There is no fixed limit, but many catch blocks often indicate that the try block does too much and should be split.
Q19. Can a catch block be empty?
It compiles, but swallowing exceptions silently is bad practice. At least log the exception, or explain in a comment why it is safe to ignore.
Q20. When was multi-catch introduced?
In Java 7 (2011), together with try-with-resources and more precise rethrow.