Multi-catch exception handling
Home / Errors / Multi-catch exception handling
Code Comparison
try {
process();
} catch (IOException e) {
log(e);
} catch (SQLException e) {
log(e);
} catch (ParseException e) {
log(e);
}
try {
process();
} catch (IOException
| SQLException
| ParseException e) {
log(e);
}
Why the modern way wins
๐
DRY
Same handling logic written once instead of three times.
๐
Rethrowable
The caught exception can be rethrown with its precise type.
๐
Scannable
All handled types are visible in one place.
Old Approach
Separate Catch Blocks
Modern Approach
Multi-catch
JDK Support
Multi-catch exception handling
Available
Widely available since JDK 7 (July 2011)
How it works
Multi-catch handles multiple exception types with the same code. The exception variable is effectively final, so you can rethrow it without wrapping.
Related Documentation
Proof