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

B.

Invalid Divisor
Successful Division 30

C.

Invalid Divisor
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

題解

第6行「aw」變數的值是10除以2的餘數,也就是0。因此第7行把值為0的「aw」變數作為除數,會拋出ArithmeticException,然後在第8行被catch接到。

接著程式執行到第14行,依然會拋出ArithmeticException,但是這個ArithmeticException會直接由main方法往外拋出給執行緒。