Top

Discussion

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?

  • A.The program will not compile.
  • B.The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
  • C.The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
  • D.The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

Answer: D

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.

No comment is present. Be the first to comment.
Loading…

Post your comment