If you have to perform different tasks at the occurrence of different Exceptions, use java multi catch block.
try{ //code that may throw exception } catch(Exception_class_Name ref){ } catch(Exception_class_Name ref){ } catch(Exception_class_Name ref){ } catch(Exception_class_Name ref){ }
public class NewMain { public static void main(String args[]){ try { int a[] = new int[5]; a[5] = 30/0; } catch(ArithmeticException e) { System.out.println(e); System.out.println("ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); System.out.println("ArrayIndexOutOfBoundsException"); } catch(Exception e) { System.out.println(e); System.out.println("Exception"); } System.out.println("rest of the code..."); } }
java.lang.ArithmeticException: / by zero ArithmeticException rest of the code...