Java Multiple Exceptions
In this page:
try {
// code
} catch (TypeA | TypeB e) {
// handle either
} catch (TypeC e) {
// handle C
}
Catching Multiple Exception Types
A single try block can be followed by multiple catch blocks, each handling a different exception type, letting a program respond differently depending on exactly which kind of error actually occurred.
Example: Catching Multiple Exception Types
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare arr and set it to new int[2]
int[] arr = new int[2];
// Print a line to the console
System.out.println(arr[5]);
// Handle ArrayIndexOutOfBoundsException if it was thrown
} catch (ArrayIndexOutOfBoundsException e) {
// Print a line to the console
System.out.println("Array error");
// Handle ArithmeticException if it was thrown
} catch (ArithmeticException e) {
System.out.println("Math error");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Multi-Catch with the Pipe Operator
Java's multi-catch syntax, using the pipe operator between exception types inside one catch block's parentheses, lets several unrelated exception types share the exact same handling code without duplicating it.
Example: Multi-Catch with the Pipe Operator
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare arr and set it to new int[2]
int[] arr = new int[2];
// Print a line to the console
System.out.println(arr[5] / 0);
// Handle ArrayIndexOutOfBoundsException | ArithmeticException if it was thrown
} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
// Print a line to the console
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Ordering Catch Blocks Correctly
When multiple catch blocks are used, a subclass exception type must always be listed before its superclass, since Java checks catch blocks in order and an earlier, broader catch would otherwise make a later, more specific one unreachable.
Example: Ordering Catch Blocks Correctly
public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[2];
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) { // subclass listed first
System.out.println("Specific catch");
} catch (Exception e) { // superclass listed after
System.out.println("General catch");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Catching a Common Superclass
Catching a general superclass like Exception or RuntimeException handles any of its subclasses with one block, which is convenient but loses the ability to react differently based on the exact exception that was thrown.
Example: Catching a Common Superclass
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (Exception e) { // catches any subclass of Exception
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
When to Use Multi-Catch
Multi-catch is the right tool when several distinct exception types genuinely deserve identical handling logic, while separate catch blocks are better when different exception types need meaningfully different responses.
Example: When to Use Multi-Catch
public class Main {
public static void main(String[] args) {
try {
Object obj = "text";
Integer n = (Integer) obj; // ClassCastException
} catch (ClassCastException | NullPointerException e) {
System.out.println("Both handled identically");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Listing a subclass and its parent in the same multi-catch, as in
catch (IOException | Exception e), which is a compile error. - Assigning to the multi-catch variable, which is a compile error since it is implicitly final.
- Putting a general catch before a specific one, which is a compile error.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: