Java SE 8 Programmer II — Question 184
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?
Answer options
- A. User is registered.
- B. An AgeOutOfLimitException is thrown.
- C. A UserException is thrown.
- D. A compilation error occurs in the main method.
Correct answer: B
Explanation
The code checks the age parameter, and since it is 60, it triggers the AgeOutOfLimitException. The UserException is not thrown because the name length is valid. There is also no compilation error, as the syntax is correct.