Exam 1z0-809 All QuestionsBrowse all questions from this exam
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 () <= 60) {

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?

    Correct Answer: A

    The code runs through the checks in the doRegister method: first, whether the name length is less than or equal to 60, which it's not since 'Mathew' has a length of 6; second, whether the age is greater than 60, which it's not since the age is 60. Therefore, neither exception is thrown, and the program executes the else block, resulting in the output 'User is registered.'.

Discussion
steefaandOption: C

C is correct.

duydnOption: C

C is correct. Coz name has length() < 60 -> throw the exception

samtash1034Option: C

c,tested