Multiple Try Catch
public class MultipleCatch{
// Handling Exception with multiple catch blocks for a try block
public static void main(String args[]){
try{
int a[] = new int[2];
a[0]=5/0; //ArithmeticException is thrown here
System.out.println("Access element three :" + a[3]);//ArrayIndexOutOfBoundsException is thrown here
}catch(ArrayIndexOutOfBoundsException e){ //ArrayIndexOutOfBoundsException is caught here
System.out.println("Exception thrown :" + e);
}catch(ArithmeticException ex){ //ArithmeticException is caught here
System.out.println("Exception thrown:" +ex);
}
System.out.println("Out of the block");
}
}
Output:
Exception thrown:java.lang.ArithmeticException: / by zero
Out of the block