Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 248

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?

    Correct Answer: D

    The method doRegister in the class App throws both UserException and AgeOutOfLimitException. However, these exceptions are already related as AgeOutOfLimitException is a subclass of UserException. Therefore, it is redundant to declare 'throws UserException, AgeOutOfLimitException'. Only UserException needs to be declared in the method's throws clause. This redundancy would result in a compilation error.

Discussion
adnano1234Option: A

AgeOutOfLimitException is thrown

steefaandOption: A

A is correct without "User is registered" string printed.

asdfjhfgjuaDCVOption: A

Age out of limit exception

iSnoverOption: A

The answer is A. AgeOutOfLimitException is thrown