Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Finding the output (Q.No. 4)
4.
What will be the output of the program?
public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A");  
        } 
        catch (RuntimeException ex) /* Line 10 */
        { 
            System.out.print("B"); 
        } 
        catch (Exception ex1) 
        { 
            System.out.print("C"); 
        } 
        finally 
        {
            System.out.print("D"); 
        } 
        System.out.print("E"); 
    } 
    public static void badMethod() 
    { 
        throw new RuntimeException(); 
    } 
}
BD
BCD
BDE
BCDE
Answer: Option
Explanation:

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.

Discussion:
20 comments Page 1 of 2.

Tushar S said:   8 years ago
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.

Sai said:   1 decade ago
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 */
{
System.out.print("B");
}

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.

Zaheer said:   1 year ago
When the main method is called, it first tries to execute the badMethod().

The badMethod() throws a RuntimeException.

Since the main method has a catch block for RuntimeException, it catches the exception and prints "B".

After the catch block, the final block is executed, which prints "D".

Finally, after the try-catch-finally block, "E" is printed. However, it is not displayed in the output because the program has already terminated due to the uncaught exception in the badMethod().

Therefore, the output of the program is "BD".

Soumyaranjan Barik said:   6 years ago
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.

Chaithanya said:   1 decade ago
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?

Tomnguyen said:   1 decade ago
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.

Gautam prusty said:   9 years ago
If any exception occurs in try block then control will go to the catch block.
try
{ badMethod();
System.out.print("A");
} here A is not printed,

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

Mukesh said:   1 decade ago
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.

Avdhesh Gupta said:   1 decade ago
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?

Vishnu said:   10 years ago
In exception handling at a time only one exception will caught, if there are more than one exception classes in catch block.


Post your comments here:

Your comments will be displayed after verification.