Javatpoint Logo
Javatpoint Logo

Java 7 Catch Multiple Exceptions

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code.

You can use vertical bar (|) to separate multiple exceptions in catch block.

An old, prior to Java 7 approach to handle multiple exceptions.

Catching Multiple Exception Types Example 1

Output:

/ by zero

Catching Multiple Exception Types Example 2

What Java 7 provides us:

Output:

/ by zero

Catching Multiple Exception Types Example 3

Output:

Compile-time error: The exception ArithmeticException is already caught by the alternative Exception

So here, in case when your are catching multiple exceptions, follow the rule of generalized to more specialized. It means that, if you are using super (general) class, don't use child (specialized) class.

Note - Catch block which handles more than one exception type makes the catch parameter implicitly final. In the above example, the catch parameter "e" is final and therefore you cannot assign any value to it.





Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials