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」。