Can we have single catch to handle multiple Exceptions

Yes, it is possible to have single catch to handle multiple Exceptions. But this syntax is supported only after Java 7 version.
Below is an example of the same.

public class MultipleExceptions
{
public static void main(String args[])
{
try {
  int j;

  int arr[] = new int[10];
  j = 42/0;
  arr[20] = 56;
} catch( ArithmeticException | IndexArrayOutOfBoundsException ex ) {
  System.out.println(ex.getMessage());
  ex.printStackTrace();
}
}
}

You may also like to read:

Leave a Reply