This is a question from javaprepare site
What is the output of the following program
public class
test {
public static void main(
String args[]) {
int i=1, j=1;
try {
i++;
j--;
if(i/j > 1)
i++;
}
catch(ArithmeticException e) {
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(1);
}
catch(Exception e) {
System.out.println(2);
}
finally {
System.out.println(3);
}
System.out.println(4);
}
}
A 0
B 1
C 2
D 3
E 4
The answer given are A,D,E. Dividing by 0 will result in Arithmetic Exception therefore A is correct. D and E are correct because finally block will always get executed and the last statement is outside try and catch block.I want to know why C is incorrect. Arithmetic Exception extends Runtime Exception which extends Exception. So C should be a valid answer.