← 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.
Syntax
java
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

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

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

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");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Listing a subclass and its parent in the same multi-catch, as in catch (IOException | Exception e), which is a compile error.
  2. Assigning to the multi-catch variable, which is a compile error since it is implicitly final.
  3. Putting a general catch before a specific one, which is a compile error.

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.