✅ Yes! Since Java 7, a single catch block can handle multiple exceptions using the pipe (|) operator. This is called multi-catch exception handling.
1. Syntax of Multi-Catch Exception Handling
try {
// Code that may throw multiple exceptions
} catch (ExceptionType1 | ExceptionType2 e) {
// Handle both exceptions
}
- The
catchblock executes if any of the specified exceptions occur. - The exception variable
eis final (cannot be reassigned).
2. Example: Catching Multiple Exceptions
✅ Example: Handling IOException and SQLException in One catch Block
import java.io.*;
import java.sql.*;
public class MultiCatchExample {
public static void main(String[] args) {
try {
throwDatabaseOrFileException();
} catch (IOException | SQLException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
public static void throwDatabaseOrFileException() throws IOException, SQLException {
if (Math.random() > 0.5) {
throw new IOException("File error");
} else {
throw new SQLException("Database error");
}
}
}
✅ Possible Outputs:
Exception caught: File error
or
Exception caught: Database error
🔹 Key Takeaway: The catch block executes for both exception types.
3. Benefits of Multi-Catch Exception Handling
✔ Reduces Code Duplication: No need to write multiple catch blocks with the same handling logic.
✔ Improves Readability: Cleaner and more concise error handling.
✔ Efficient Exception Handling: Prevents redundant catch statements.
4. Restrictions on Multi-Catch
🚫 1. Cannot Catch Related Exceptions (Subclass & Superclass Together)
try {
// Code that may throw exceptions
} catch (IOException | Exception e) { // ❌ Compilation Error: IOException is a subclass of Exception
System.out.println("Cannot catch subclass and superclass together");
}
✅ Solution: Catch the superclass (Exception) alone:
try {
// Code that may throw exceptions
} catch (Exception e) { // ✅ Works fine
System.out.println("Handled all exceptions");
}
🚫 2. The Exception Variable Is final
catch (IOException | SQLException e) {
e = new IOException(); // ❌ Compilation Error: Variable 'e' is final
}
✅ Solution: Do not reassign e.
5. Alternative: Using Multiple catch Blocks
If you need different handling for different exceptions, use separate catch blocks.
✅ Example: Handling Different Exceptions Separately
try {
throw new IOException("File not found");
} catch (IOException e) {
System.out.println("IOException caught: " + e.getMessage());
} catch (SQLException e) {
System.out.println("SQLException caught: " + e.getMessage());
}
6. Summary
| Feature | Multi-Catch (|) | Multiple catch Blocks | |———|——————|——————| | Reduces Code Duplication | ✅ Yes | ❌ No | | Handles Different Exceptions Separately | ❌ No | ✅ Yes | | Supports Subclass & Superclass Together? | ❌ No | ✅ Yes | | Best for | Same handling for multiple exceptions | Different handling for each exception |
Final Thoughts
✔ Yes, a single catch block can handle multiple exceptions using |.
✔ Avoid catching superclass and subclass exceptions together.
✔ Use multiple catch blocks if different handling is needed.