The following code gives the desired output which is "Arithmetic Exception occurs"
public static void main(String[] args) {
try {
int []a = new int[5];
a[5] = 30/0;
} catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception occurs");
} catch(Exception e) {
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
but if I want to get two exceptions at a time, the modified code will be
public static void main(String[] args) {
try {
int []a = new int[5];
a[10] = 30/0;
} catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception occurs");
} catch(Exception e) {
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
but instead of giving both, "Arithmetic Exception occurs" & "ArrayIndexOutOfBounds Exception occurs"
the output is only "Arithmetic Exception occurs"
Can anyone explain this?