Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Multi-Catch: Catch Multiple Exceptions Cleanly

Java multi-catch lets you handle multiple exception types in one catch block. See syntax, valid combinations, rethrowing, and when separate blocks are better.

~4 min readJava
#exception handling#multi-catch#Java 7#error handling
Illustration of Java multi-catch exception handling with multiple exception types merging into a single catch block.

Java multi-catch, introduced in Java 7, lets you handle multiple exception types in a single catch block when those types are not subclasses of each other. It reduces repetitive catch blocks and makes error handling more readable. Instead of writing several catch blocks that run the same logic, you can group the exception types with a pipe (|) and share one handler.

Basic Syntax of Multi-Catch

The syntax follows the standard try/catch structure, but the catch parameter lists multiple exception types separated by |:

java
try { // code that may throw different exceptions } catch (IOException | SQLException e) { // shared handling logic System.err.println("Operation failed: " + e.getMessage()); }

The exception variable e is implicitly final. You cannot assign a new value to it inside the block, which prevents accidental reassignment and keeps behavior predictable.

How Multi-Catch Handles Exception Types

A multi-catch block behaves like one catch block whose type is a union of the listed exceptions. It matches when the thrown exception is assignable to any of the listed types. Because the match is based on the actual exception class, a subclass of one of the listed types is also caught. For example, FileNotFoundException is a subclass of IOException, so catch (IOException | SQLException e) will catch it.

The alternatives inside a multi-catch are not ordered; writing IOException | SQLException is the same as SQLException | IOException. If you have multiple catch blocks, however, they are still matched in order, so place more specific catch blocks before broader ones.

The Exception Variable Is Implicitly Final

In a multi-catch block, the exception variable is implicitly final. This means you cannot reassign it, and you can reference it from an inner class or lambda if needed. Attempting to assign a new value is a compile error:

java
try { // risky code } catch (IOException | SQLException e) { e = new IOException(); // compile error: cannot assign a value to final variable e }

This design avoids ambiguity about which exception is being handled. If you need to wrap the exception, create a new variable instead.

When Multi-Catch Cannot Be Used

You cannot combine exception types in the same multi-catch block when one is a subclass of another. The compiler rejects the combination because the handler would be redundant and the alternatives would overlap. For example, this is invalid:

java
try { // code } catch (FileNotFoundException | IOException e) { // error: alternatives in a multi-catch statement cannot be related by subclassing }

FileNotFoundException is already an IOException, so the only multi-catch form that makes sense is catch (IOException e). The rule prevents code that implies distinct handling where none exists.

Multi-Catch vs. Separate Catch Blocks

Separate catch blocks allow different handling per exception type. Multi-catch is appropriate when the handling logic is identical. Consider this scenario where you read a configuration file and parse a number:

java
// Separate catch blocks catch (IOException e) { logError("Failed to read config", e); } catch (NumberFormatException e) { logError("Invalid number in config", e); } // Multi-catch catch (IOException | NumberFormatException e) { logError("Failed to process config", e); }

If the actions are the same, multi-catch reduces duplication. If you need to differentiate the error message or recovery steps, separate blocks are clearer. Multi-catch does not replace separate blocks; it complements them for cases where the response is identical.

Using Multi-Catch with Custom Exceptions and Re-throwing

Multi-catch works with custom exception classes as long as they are not in a subclass relationship. You can also rethrow the exception from a multi-catch block. Because the catch parameter is final, Java's precise rethrow analysis can keep the declared exception types narrow:

java
public void process() throws IOException, SQLException { try { // risky operations } catch (IOException | SQLException e) { log(e); throw e; // compiler can infer IOException or SQLException } }

This is more useful than declaring a broad Exception type. The method signature reflects the actual exceptions, which helps callers handle them appropriately.

Performance and Maintainability Considerations

Multi-catch is primarily a source-level feature. The JVM's exception table handles the combined handler without extra runtime dispatch compared with the equivalent separate catch blocks. The maintainability gain is more important: shared handling logic appears once, so you do not have to keep several catch blocks in sync.

A subtle limitation is that the exception variable's static type is the least upper bound of the listed exceptions, not one of the individual types. For IOException | SQLException, e has type Exception, so you can call only methods available on Exception. If you need IOException- or SQLException-specific methods, use separate catch blocks or cast carefully.

Common Mistakes and Edge Cases

  • Listing types where one is a subclass of another in the same multi-catch block.
  • Trying to assign a new value to the exception variable.
  • Expecting an exception from a different try block to be caught by a multi-catch; each try block needs its own catch handling.
  • Assuming multi-catch changes catch order. With multiple catch blocks, the first matching block still wins; within a multi-catch, the alternatives are not ordered.

Use multi-catch when the handling response is genuinely shared. If the logic diverges, separate blocks are clearer. The feature is most useful in I/O operations, parsing, and code that interacts with multiple APIs that throw unrelated checked exceptions.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.