Given the following code for the classes MyException and Test:
What is the result?
Given the following code for the classes MyException and Test:
What is the result?
Given the provided code, the output of the program can either be 'A' or 'B'. Here's the reasoning: In the main method, method1() is called within a try block. In method1(), there's a 50% chance that a MyException will be thrown, and a 50% chance that a RuntimeException will be thrown, as determined by Math.random(). Both exceptions are caught by the catch block in method1(), which prints 'B'. Therefore, whenever an exception is thrown in method1(), 'B' is printed. However, if MyException is thrown, it is not rethrown to the main method. Consequently, the catch block in the main method will not be executed, and 'A' will not be printed. The correct answer is that either 'A' (if MyException is thrown and caught) or 'B' (if RuntimeException is thrown and caught). Therefore, the result is either A or B.
I agree, the correct answer B B
B is only correct answers, as MyException is subclass of RuntimeException, so it will be caught by the catch block in the method1.
Answer is C!!!!!
wrong. MyException extends RuntimeException
wrong. for both classes MyException or RuntimeException, we'll have 'B' printed since MyException is a subclass of RuntimeException. Thanks for rectifying...
yes it is!
correct answer is B class MyException extends RuntimeException{} public class Test{ public static void main(String[] args) { try { method1(); } catch (MyException e) { System.out.println("A"); } } public static void method1() { try { throw new MyException(); } catch (RuntimeException e) { System.out.println("bb"); } } }
It is B. Runtime catches MyException too.
the correct answer B
exception is caught in method, B.
THE ANSWER IS B
Tested. Answer is B. Source code: class MyException extends RuntimeException{} public class Test { public static void main(String[] args) { try { method1(); } catch (MyException e) { System.out.println("A"); } } public static void method1() { try { throw Math.random() > 0.5 ? new MyException() : new RuntimeException(); } catch (RuntimeException re) { System.out.println("B"); } } }
B is correct beacause MyException extends Runtime exception.
B is Correct
Answer is B. Because MyException is RuntimeException.
correct answer is B because MyException is RuntimeException itself.
E. compilation error at line n1 "Exception" needs to be declared
nope. RuntimeException as any class extenting it is an unchecked exception, plus it is already hadled in a try block so double wrong
B , catch (RuntimeException re)” always catches a RuntimeException