Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Finding the output (Q.No. 3)
3.
What will be the output of the program?
public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A"); 
        }  
        catch (Exception ex) 
        {
            System.out.print("B");  
        } 
        finally 
        {
            System.out.print("C"); 
        } 
        System.out.print("D"); 
    }  
    public static void badMethod() 
    {
        throw new Error(); /* Line 22 */
    } 
}
ABCD
Compilation fails.
C is printed before exiting with an error message.
BC is printed before exiting with an error message.
Answer: Option
Explanation:

Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).

Discussion:
29 comments Page 3 of 3.

Bindu said:   7 years ago
Why A is not printed here? Please explain in detail.

Prithiv said:   9 years ago
I didn't get this, Can anyone explain this clearly?

Matikana Rambabu said:   7 years ago
Can anyone explain why "D" is not printed? Please?
(1)

Sonika said:   9 years ago
What is the relation between error and exception?

Ammu said:   9 years ago
Can't understand. So please explain me clearly.

Vijay said:   8 years ago
I couldn't get it, can someone help me out?

Khagendra said:   8 years ago
Thanks @Anoms for brief explanation.

James said:   9 years ago
Why is it not printing "A" here?

Divyaja said:   7 years ago
Why D is not printed?


Post your comments here:

Your comments will be displayed after verification.