Given:
public class Test {
public static void main(String[] args) {
int ax = 10, az = 30;
int aw = 1, ay = 1;
try {
aw = ax % 2;
ay = az / aw;
} catch (ArithmeticException e1) {
System.out.println("Invalid Divisor");
} catch (Exception e2) {
aw = 1;
System.out.println("Divisor Changed");
}
ay = az / aw; // Line 14
System.out.println("Succesful Division " + ay);
}
}
What is the result?
A.
Invalid Divisor
Divisor Changed
Successful Division 30
Divisor Changed
Successful Division 30
B.
Invalid Divisor
Successful Division 30
Successful Division 30
C.
Invalid Divisor
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Teagle.main(Teagle.java:14)
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Teagle.main(Teagle.java:14)
D.
Invalid Divisor
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Teagle.main(Teagle.java:14)
Successful Division 1
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Teagle.main(Teagle.java:14)
Successful Division 1
题解
第 6 行「aw」变量的值是 10 除以 2 的余数,也就是 0。因此第 7 行把值为 0 的「aw」变量作为除数,会抛出 ArithmeticException,然后在第 8 行被 catch 接到。
接着程序运行到第 14 行,依然会抛出 ArithmeticException,但是这个 ArithmeticException 会直接由 main 方法往外抛出给线程。
