← Back to Core Java Course | Chapter 10: Exception Handling | Lesson 3 of 8

Java Multiple Exceptions

A single try block can be followed by multiple catch blocks, or use the pipe operator to catch several exception types in one block, letting a program respond precisely to different kinds of errors.

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

java
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");
		}
	}
}

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

java
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());
		}
	}
}

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

java
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");
		}
	}
}

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

java
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());
		}
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.