What will the output of the following program be? Public class - ProProfs Discuss
Topics
Products
Follow Us:

What will the output of the following program be?
Public class MyClass {public static void main(String[] args) {try {f();} catch (InterruptedException e) {System.out.println("1");throw new RuntimeException();} catch (RuntimeException e) {System.out.println("2");return;} catch (Exception e) {System.out.println("3");} finally {System.out.println("4");}System.out.println("5");}// InterruptedException is a direct subclass of Exception.static void f() throws InterruptedException {throw new InterruptedException("Time for lunch.");}}



A. The program will print 5.
B. The program will print 1and 4, in that order.
C. The program will print 1,2, and 4, in that order.
D. The program will print 1,4, and 5, in that order.
E. The program will print 1,2, 4, and 5, in that order
F. The program will print 3and 5, in that order.

This question is part of Exception Handling
Asked by RiaTor, Last updated: Mar 13, 2020

+ Answer
Request

1 Answer

John Smith

John Smith

Answered Jan 07, 2017

The program will print 1and 4, in that order.

(b) The program will print 1and 4, in that order. An InterruptedExceptionis handled in the first catchblock. Inside this block a new RuntimeExceptionis thrown. This exception was not thrown inside the tryblock and will not be handled by the catch blocks, but will be sent to the caller of the main()method. Before this happens, the finallyblock is executed. The code to print 5is never reached, since the RuntimeExceptionremains uncaught after the execution of the finallyblock.
 

Search for Google images
Select a recommended image
Upload from your computer
Search for Google images
Select a recommended image
Upload from your computer
Search for Google images
Select a recommended image
Upload from your computer

Email Sent
We have sent an email to your address "" with instructions to reset your password.