What is the output of the following application?
package mypkg; // w w w. j a v a2s . c om public class Main { public static void m() { try { String sheep[] = new String[3]; System.out.print(sheep[3]); } catch (RuntimeException e) { System.out.print("Awake!"); } finally { throw new Exception(); // x1 } } public static void main(String... sheep) { // x2 new Main().m(); // x3 } }
B.
The finally block of the m()
method throws a new checked exception on line x1,
but there is no try-catch block around it to handle it,
nor does the m()
method declare any checked exceptions.
Line x1 does not compile, and Option B is the correct answer.
The rest of the lines of code compile without issue, even line x3 where a static method is being accessed using an instance reference.
The code inside the try block, if it ran, would produce an ArrayIndexOutOfBoundsException, which would be caught by the RuntimeException catch block, printing Awake!.