Exceptions: multi-catch handles two unrelated exception types in one block

A single catch clause can list several exception types separated by pipes, running one handler for any of them; the caught variable is effectively final and typed as the common ancestor of the listed types.

Code
for (String input : new String[]{"12", "oops"}) {
    try {
        if (input.equals("oops")) {
            throw new java.io.IOException("read failed");
        }
        System.out.println("Parsed: " + Integer.parseInt(input));
    } catch (NumberFormatException | java.io.IOException e) {
        System.out.println("Handled " + e.getClass().getSimpleName() + ": " + e.getMessage());
    }
}
Output
Parsed: 12
Handled IOException: read failed
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms