Given the following code for the classes MyException and Test:
What is the result?
A. A
B. B
C. Either A or B
D. A B
E. A compile time error occurs at line n1
Given the following code for the classes MyException and Test:
What is the result?
A. A
B. B
C. Either A or B
D. A B
E. A compile time error occurs at line n1
Answer B:
It will always print “B”, because the method1() method catches any RunTimeException, including the child exception class, MyException.
Answer is B
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”);
}
}
}
class MyException extends RuntimeException{}
Test.java:15: error: illegal start of expression
public static void method1(){
should be E
nvm it’s b