Multi-Catch Exception Handling in Java 7
Fast recall
Quick revision
Core answer
catch handle several exception types using the pipe operator: catch (IOException | SQLException e). It removes duplicated handling code. Two rules: the variable e is implicitly `final` (you can't reassign it), and the listed types can't be parent-child (catch (Exception | IOException) won't compile because it's redundant).Awareness of the Java 7 multi-catch syntax and its two compiler rules — implicitly final variable and the no-parent-child restriction
Not knowing multi-catch exists and writing duplicate catch blocks, or trying to list a type and its supertype together
Explain WHY the variable is final (the runtime type could be any of the listed types) and why parent-child is rejected as redundant
Interview-ready guide
Interview answer
Yes, and the feature is called multi-catch — it arrived in Java 7.
The idea is that instead of writing two separate catch blocks with the same body, you list the exception types in a single catch separated by a pipe character, like catch I O exception pipe number format exception. Then that one block handles either type, which removes the copy-paste duplication you'd otherwise have. There are two rules the interviewer is usually probing for.
The first is that the caught variable is implicitly final, so you can't reassign it inside the block. The reason is that at runtime the actual exception could be any of the listed types, so Java treats the variable as read-only to keep things unambiguous. The second rule is that the types you list can't be in a parent-child relationship.
If you write catch exception pipe I O exception, that's a compile error, because I O exception is already a subclass of exception, so listing both is redundant. You can only combine types that are independent branches of the hierarchy.
One more detail worth knowing is that the static type of the variable is the nearest common supertype of everything you listed, so if you combine two unrelated checked exceptions you'll effectively be working with Exception, and you can only call methods that exist on that common parent, like getMessage. The practical guidance I'd give is to use multi-catch only when the handling is genuinely identical.
If one exception should trigger a retry and another should fail immediately, keep them in separate catch blocks so the intent stays clear.
Multi-Catch Exception Handling in Java 7
Direct Answer
Yes — since Java 7, multi-catch lets one catch handle several exception types using the pipe operator: catch (IOException | SQLException e). It removes duplicated handling code. Two rules: the variable e is implicitly final (you can't reassign it), and the listed types can't be parent-child (catch (Exception | IOException) won't compile because it's redundant).
Common Mistakes & Pitfalls
- Not knowing it exists — writing N identical catch blocks instead of one multi-catch.
- Listing a type and its supertype —
catch (Exception | IOException)is a compile error (redundant). - Trying to reassign
e— it's implicitlyfinal;e = new ...won't compile. - Forcing unrelated exceptions together — if
IOExceptionshould retry butSQLExceptionshould fail, keep separate blocks; multi-catch is only for identical handling.
Check your understanding
Answer these without looking back at the article.
- Why is the exception variable final in a multi-catch block?
- What happens if you try to catch Exception | IOException in a multi-catch?
- How is multi-catch different from catching the common parent exception?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.