Friday, January 31, 2014

How to handle more than one type of exception?

Source:
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

Blog Archive