Catching Multiple Exceptions in Java

Catching multiple exceptions in java is now very easy starting from Java 7.
Before java 7 we used to write the below code for catching multiple exceptions

But since Java 7 we can easily club some of the exceptions,so that code redundancy can be avoided
Note that we use a vertical bar (| symbol ) to add multiple exceptions in one catch block.

Example: Catching multiple exceptions in java

Advantages of Caching Multiple exception

  1. Bytecode generated for catch block that handles multiple exception types is smaller than many catch blocks handling one exception per block.
  2. Code redundancy or code duplication can be avoided by catch block that handles multiple exception types.

Reference

JDK 7 release notes

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn