Given:

class UserException extends Exception {
}

class AgeOutOfLimitException extends UserException {
}

and the code fragment:

class App {

    public void doRegister(String name, int age) throws UserException, AgeOutOfLimitException {
        if (name.length() < 6) {
            throw new UserException();
        } else if (age >= 60) {
            throw new AgeOutOfLimitException();
        } else {
            System.out.println("User is registered.");
        }
    }

    public static void main(String[] args) throws UserException {
        App t = new App();
        t.doRegister("Mathew", 60);
    }
}

What is the result?

A.

User is registered.

B. An AgeOutOfLimitException is thrown.
C. A UserException is thrown.
D. A compilation error occurs in the main method.

題解

由於第 23 行呼叫 App 物件的 doRegister 方法所傳入的 age 參數為 60,因此第 14 行的 if 條件式會成立,所以在執行第 15 行時拋出 AgeOutOfLimitException。