What is the result of compiling and running the following application?
package mypkg; /*from w w w . ja va 2 s . co m*/ public class Main { public void m() throws Exception { // p1 try { throw new Exception("Circle"); } catch (Exception e) { System.out.print("Opening!"); } finally { System.out.print("Walls"); // p2 } } public static void main(String[] moat) { new Main().m(); // p3 } }
C.
The application does not compile, so Option D is incorrect.
The m()
method compiles without issue, so Options A and B are incorrect.
The issue here is how the m()
method is called from within the main()
method on line p3.
The m()
method declares the checked exception, Exception, but the main()
method from which it is called does not handle or declare the exception.
In order for this code to compile, the main()
method would have to have a try-catch statement around line p3 that properly handles the checked exception, or the main()
would have to be updated to declare a compatible checked exception.
Line p3 does not compile, and Option C is the correct answer.