Java Programming - Exceptions - Discussion


What will be the output of the program?

public class X 
    public static void main(String [] args) 
        catch (RuntimeException ex) /* Line 10 */
        catch (Exception ex1) 
    public static void badMethod() 
        throw new RuntimeException(); 

[A]. BD
[B]. BCD
[C]. BDE

Answer: Option C


A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.

Priyanka said: (Jun 4, 2013)  
If exception is not caught then all the code after finally statement is run or not?

Laxmi said: (Jun 24, 2013)  
Why not A got printed. Because after exception caught, program should follow natural flow?

Mohan said: (Sep 15, 2013)  
I have also same question why A was not printed?

Tomnguyen said: (Dec 24, 2013)  
After an exception has been caught, the rest of the code in try-catch block will not be executed, and other code outside the try-catch block will be executed. Note that the code inside finally block will run once any part of the code in try{} block has been run.

Chaithanya said: (Mar 12, 2014)  
After method is called and exception is thrown, control transfers to catch block and never returns to try block so sop is not executed in try block

Exception is handled but sop(b) is not in output.

@Tomnguyen. You said it won't execute in catch block too...what about System.out.println(e) which returns exception when used in catch block?

Sai said: (May 9, 2014)  
Hi Mohan,

Before System.out.print("A").. one badMethod() is there. when compiler found an exception, further codes wont be execute.thats why A is nt print. it will execute if that exception will be caught by catch block. So this RuntimeException is caught in catch (RuntimeException ex) /* Line 10 */

Thats why b is printed. and finally block will always printed. and the rest of the stmt also printed because the exception has already been caught. I hope you understood.

Mukesh said: (Aug 24, 2014)  
Thank you @Sai,

But I have one more doubt why it not prints "C". An RuntimeException is caught but it should also be caught in super class java.lang.Exception .

I am waiting for your reply.

Aamir said: (Jan 24, 2015)  
Hi @Mukesh.

If the Exception is caught then the catch try and catch block no compile.

Avdhesh Gupta said: (Mar 2, 2015)  
When I write throw new Exception() I have to add to the method throws but when I throw new Runtime Exception() I don't have to mention throws, Why?

Rasika said: (Jul 24, 2015)  
The options mentioned are wrong!

Runtime Exception is subclass of Exception so above piece of code will not compile.

Vishnu said: (Dec 11, 2015)  
In exception handling at a time only one exception will caught, if there are more than one exception classes in catch block.

Anusha said: (Mar 17, 2016)  
Can any explain clearly.

Gautam Prusty said: (Apr 25, 2016)  
If any exception occurs in try block then control will go to the catch block.
{ badMethod();
} here A is not printed,

If you have not understood this means ask me, I will help you.

Vikram said: (Jun 17, 2016)  
Define badMethod().

Parthipan said: (Jun 24, 2016)  
Hi, I'm not clear with the given explanation, can any help to to understand this?

Mounika said: (Sep 16, 2016)  
Can anyone clearly explain the entire program?

Ayushisa36491 said: (Sep 24, 2016)  
Line 10 has been declared as a comment, but then also exception is caught, how that suppose to take place?

Tushar S said: (Mar 21, 2017)  
If any program Exception occurs in try-block then 1) program abnormally terminated.2)Control comes out from program 3)JVM does not execute remaining try-block statement.

Control goes to Catch-block and Check any appropriate Exception class object created or not. if created then execute these Catch-block Statement.(above prog. object of RuntimeException() re)

After Executing Catch-block statement. Control does not go to try-block, but its control goes to finally block if present in the program.

After finally, block executing, control goes to the remaining statement (or logic).

In above prog, Inside static badMethod() method create the object of RuntimeException(), whenever call the method, then create the Exception i.e RuntimeException(), and goes control to catch-block and execute the statement and another block like finally.

Soumyaranjan Barik said: (Aug 28, 2019)  
Before System.out.print("A").. one badMethod() is there, when flow will go to the try block badMethod() will throw *RuntimeException* , which must be catch by handler. So it will move to catch block//

catch (RuntimeException ex) /* Line 10 */

In line 10 its handled. So flow will forward next (it will print B) and one more Exception handled which has no use because No Exception is there.

So it will Execute Finally block (it will Print DE)
And the Answer is BDE.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.