What is the Java multi-catch clause and how do you write one?
answer
- | (pipe) separates types
- one shared body, one variable
- Java 7 feature
- removes duplicated catch blocks
- variable typed as common supertype
basics
~10 sMulti-catch lets one catch block handle several exception types. You list the types separated by a vertical bar (|), like catch (IOException | SQLException e), and write a single shared body.
solid answer
~40 sMulti-catch, added in Java 7, lets a single catch block handle more than one exception type by separating the types with the | (pipe) operator: catch (IOException | SQLException e) { ... }. It exists to remove duplicated handling code — before it, you needed a separate catch block per type even when the recovery logic was identical, or you had to catch a broad common supertype and lose specificity. The handler body runs for any of the listed types. The exception variable (e here) has, as its static type, the most specific common supertype of the listed types, so inside the block you can only call methods declared on that common type. It reads left to right and there is exactly one variable name at the end.
code
java · 6 linestry {
parseAndStore(input);
} catch (NumberFormatException | SQLException e) {
log.error("processing failed", e); // shared handling, no duplication
throw new ProcessingException(e);
}go deeper
Can write a multi-catch clause with the | operator and explain it handles several exception types in one block.
Knows the variable is typed as the common supertype and that it removes duplication without widening to a catch-all.
Articulates the design trade-off vs. catching a broad supertype, and the readability/precision benefit; mentions it pairs with try-with-resources from Java 7.
Frames multi-catch within an exception-handling strategy: when to consolidate handlers, how it interacts with effectively-final analysis and exception transparency in generic/rethrow code.
## What an exception and a catch block are In Java, when something goes wrong at runtime (a file is missing, a number can't be parsed, a database call fails), the code *throws* an **exception** — an object describing the error. To deal with it you wrap risky code in a `try` block and follow it with one or more `catch` blocks. Each `catch` names an exception **type**; if the thrown object is an instance of that type, that block runs. ```java try { risky(); } catch (IOException e) { // handle I/O failures } ``` ## The problem multi-catch solves Before Java 7, each `catch` could name only **one** type. If two different exceptions needed the *same* handling, you had to repeat the body: ```java try { risky(); } catch (IOException e) { log.error("failed", e); throw new ServiceException(e); } catch (SQLException e) { log.error("failed", e); // duplicated throw new ServiceException(e); // duplicated } ``` The only alternatives were ugly: catch a broad common supertype like `Exception` (which also swallows unrelated errors you didn't mean to handle), or factor the body into a private method (extra noise). Both hurt readability. ## The multi-catch syntax Java 7 added the **multi-catch** clause. You list several types in one `catch`, separated by the `|` (vertical bar / pipe) operator, and give a single body: ```java try { risky(); } catch (IOException | SQLException e) { log.error("failed", e); throw new ServiceException(e); } ``` Rules of the syntax: - The types are separated by `|`, not commas. - There is exactly **one** variable name, written **once**, after the last type (`e` above). - The block runs if the thrown exception is an instance of **any** of the listed types. - You can list two, three, or more types. ## Type of the exception variable Inside the block, the static (compile-time) type of the variable is the **most specific common supertype** of all the listed types. For `IOException | SQLException`, both extend `Exception`, so `e` is typed as `Exception`, and you may only call methods that `Exception` declares (such as `getMessage()`). You cannot call a method that exists only on `SQLException`, because the compiler can't prove the caught object is a `SQLException`. ## Why it matters Multi-catch is purely about **eliminating duplicated handling code** while keeping handling **precise** — you name exactly the types you mean, instead of widening to a catch-all. It is a compile-time/syntax feature; it changes nothing about how exceptions are thrown or propagated.
- What is the static type of the exception variable in catch (IOException | SQLException e)?Exception — the most specific common supertype of the two listed types, so only members of Exception are callable on it.
- In which Java version was multi-catch introduced?Java 7 (2011), as part of Project Coin alongside try-with-resources.
saying these in an interview costs you the question
- Saying you separate the types with commas instead of |
- Thinking you write the variable name after each type
- Believing multi-catch lets you call subtype-specific methods on the variable
- Confusing it with chaining multiple separate catch blocks