Multiple choice

public class MyProgram { public static void throwit() { throw new RuntimeException(); } public static void main(String args[]) { try { System.out.println("Hello world "); throwit(); System.out.println("Done with try block "); } finally { System.out.println("Finally executing "); } } } which answer most closely indicates the behavior of the program?

  1. The program will not compile.

  2. The program will print Hello world, then will print that a RuntimeExceptionhas occurred, then will print Done with try block, and then will print Finally executing.

  3. The program will print Hello world, then will print that a RuntimeExceptionhas occurred, and then will print Finally executing.

  4. The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Once the program throws a RuntimeException (in the throwit() method) that is not caught, the finally block will be executed and the program will be terminated. If a method does not handle an exception, the finally block is executed before the exception is propagated.