Given:
public class Test {
static void dispResult(int[] num) {
try {
System.out.println(num[1] / (num[1] - num[2]));
} catch (ArithmeticException e) {
System.err.println("First exception");
}
System.out.println("Done");
}
public static void main(String[] args) {
try {
int[] arr = {100, 100};
dispResult(arr);
} catch (IllegalArgumentException e) {
System.err.println("Second exception");
} catch (Exception e) {
System.err.println("Third exception");
}
}
}
What is the result?
A.
0
Done
Done
B.
First Exception
Done
Done
C.
Second Exception
D.
Done
Third Exception
Third Exception
E.
Third Exception
题解
程序第 5 行会因为 num 所参考到的数组对象并没有索引 2 这个位置,而抛出 ArrayIndexOutOfBoundsException。而这个 ArrayIndexOutOfBoundsException 并不是 ArithmeticException,所以不会被第 6 行的 catch 接到,而由「dispResult」方法继续向外抛出,最终被第 18 行的 catch 接到,输出「Third exception」。
