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.
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());
}
}
Parsed: 12
Handled IOException: read failed
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