javacodex.com
Java Examples
Java Examples
memu home questions

Catch Multiple Exceptions At Once

In Java SE 7 and later, a single catch block can handle more than one type of exception.

This java example demonstrates how to catch more than one type of exception with one exception handler.

Source: (MultipleExceptions.java)

//Handling more than one type of Exception in one catch()
 
public class MultipleExceptions {
 
   public static void main(String[] args) {
 
      try {
        int x = 10/0;
      } catch (NullPointerException|ArithmeticException ex) {
         System.out.println("NullPointer or Arithmetic caught");
      }
   }
}
 

Output:

$ java MultipleExceptions 
NullPointer or Arithmetic caught

Contact: javacodex@yahoo.com