Java Multiple Exceptions
In this page:
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
public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[2];
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array error");
} 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
public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[2];
System.out.println(arr[5] / 0);
} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
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
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: