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
B
Indeed, the answer is B. Every exception (MyExp. or RunRep.) is always caught by RuntimeException because it is the parent of MyException.
package q047;
public class MyException extends RuntimeException {}
package q047;
public class Test {
public static void main(String[] args) {
try {
method1();
} catch (MyException me) {
System.out.print(“A”);
}
}
public static void method1() {//line n1
try {
throw Math.random() > 0.5 ? new MyException() : new RuntimeException();
} catch (RuntimeException re) { //RuntimeE is parent of MyException
System.out.print(“B”);
}
}
}
B