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。