Errors beginner

Multi-catch exception handling

Catch multiple exception types in a single catch block.

✕ Pre-Java 7
try {
    process();
} catch (IOException e) {
    log(e);
} catch (SQLException e) {
    log(e);
} catch (ParseException e) {
    log(e);
}
✓ Java 7+
try {
    process();
} catch (IOException
    | SQLException
    | ParseException e) {
    log(e);
}
📏

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
Since JDK
7
Difficulty
beginner
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.

Share 𝕏 🦋 in