What is the result of compiling and running the following application?
package mypkg; //w w w. j a v a 2s . com class Exception1 extends Exception {} class Exception2 extends Exception1 {} public class Main { public void m() throws RuntimeException { // q1 try { throw new Exception2(); } catch (Exception e) { throw new ClassCastException(); } finally { throw new Exception1(); // q2 } } public static void main(String[] moat) { new Main().m(); // q3 } }
B.
The application does not compile, so Option D is incorrect.
The checked Exception2 thrown in the try block is handled by the associated catch block.
The ClassCastException is an unchecked exception, so it is not required to be handled or declared and line q1 compiles without issue.
The finally block throws a checked Exception1, which is required to be handled or declared by the method, but is not.
There is no try-catch around line q2, and the method does not declare a compatible checked exception, only an unchecked exception.
For this reason, line q2 does not compile, and Option B is the correct answer.
line q3 compiles without issue because the unchecked RuntimeException is not required to be handled or declared by the call in the main()
method.