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?
Click on the arrows to vote for the correct answer
A. B. C. D.B.
The code defines a class App
with a doRegister
method that takes in a String
parameter name
and an int
parameter age
. The method throws two checked exceptions UserException
and AgeOutOfLimitException
. The doRegister
method checks if the length of the name
is less than 6 and if so, it throws a UserException
. If the age
is greater than or equal to 60, it throws an AgeOutOfLimitException
. If neither of these conditions is true, it prints "User is registered."
In the main
method, an instance of the App
class is created and its doRegister
method is called with the parameters "Mathew" and 60. The main
method declares that it throws UserException
, which means it must either handle the exception or declare that it throws it.
In this case, since the doRegister
method throws both UserException
and AgeOutOfLimitException
, the main
method needs to declare that it throws both of these exceptions. However, it only declares that it throws UserException
, which means that the code will not compile and a compilation error will occur in the main
method.
Therefore, the correct answer is D. A compilation error occurs in the main method.