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


/ by zero

Catching Multiple Exception Types Example 2

What Java 7 provides us:


/ by zero

Catching Multiple Exception Types Example 3


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.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA