Java Programming - Exceptions - Discussion

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 */
    } 
}

[A]. ABCD
[B]. Compilation fails.
[C]. C is printed before exiting with an error message.
[D]. BC is printed before exiting with an error message.

Answer: Option C

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).


Amit Kumar Singh said: (Jul 7, 2011)  
I want to write a program if I enter a number it will check whether the the number is the power of two or not I want a complete program please reply soon.

Sundar said: (Jul 7, 2011)  
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int n)
{
if(n == 0)
return 0;
while(n != 1)
{
n = n/2;
if(n%2 != 0 && n != 1)
return 0;
}
return 1;
}


Call the above function from your code. That's all.

Kleen said: (Sep 28, 2011)  
if(n&&(n-1)==0)
{
//power of 2
}
else
{
//not a power of 2
}

Sahil said: (Nov 15, 2011)  
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 */
}
}

Now why output ACD plz explain catch not excute?

Siddhu said: (Dec 20, 2012)  
@Sahil

You made line 22 as comment. So the fn throw new Error(); is not bother.

No error has occurred (exception) and further the return type is void so catch does not work at all.

So it execute the finally block and further on.

Akhilesh said: (Oct 14, 2013)  
Answer is C because Error class is not extends exception class it extends Throwable interface.

Ram said: (Oct 25, 2013)  
The answer is B.

Compilation fail because it complain to required Throwable instead of Exception in catch block. TRY IT.

Steve said: (Feb 14, 2014)  
Why does System.out.print("D"); not execute?

Sonali said: (Nov 11, 2014)  
If in this code I want to execute catch statement then what changes I have to make?

And what is the meaning by this that Exception is not a super class of error();

Suresh said: (Mar 18, 2015)  
Exception is not a super class of error means error class is not extends Exception. That is reason catch block is not executing directly it executes finally block.

Hritika said: (Sep 29, 2015)  
Isn't is mandatory to use throws keyword with bad method () if throw keyword is used in the method definition. ?

Prithiv said: (Jul 13, 2016)  
I didn't get this, Can anyone explain this clearly?

James said: (Jul 13, 2016)  
Why is it not printing "A" here?

Sonika said: (Aug 28, 2016)  
Anyone exlain the hierarchy of exception handling for me please.

Sonika said: (Aug 28, 2016)  
What is the relation between error and exception?

Suma said: (Dec 2, 2016)  
@Sonika.

Throwable is the parent class, exception and error are subclass of throwable.

Sachith said: (Dec 13, 2016)  
Can anyone explain why this line not execute "System.out. print("D") ;"?

Ammu said: (Jan 26, 2017)  
Can't understand. So please explain me clearly.

Vijay said: (May 13, 2017)  
I couldn't get it, can someone help me out?

Anoms said: (May 21, 2017)  
Here is an explanation why 'C' is the answer.

1. The class is instantiated.
2. Going to try() block.
3. badMethod() is called.
4. badMethod() throwing an Error.
5. Going back to try() block, it doesn't have anything to handle the Error because Exceptions and Errors two separate things.
6. The catch (Exception ex) only catches exceptions, not errors.
7. Finally() block is executed...ALWAYS!!!(No matter what happened in the try() & catch() blocks).
8. C is printed then the error message get printed.

Murali said: (Jun 17, 2017)  
I have executed the above program.

And I got output only error message and "C" is not printed.

Can anyone explain the flow?

Thank you.

Khagendra said: (Nov 18, 2017)  
Thanks @Anoms for brief explanation.

Divyaja said: (Apr 1, 2018)  
Why D is not printed?

Bindu said: (Apr 26, 2018)  
Why A is not printed here? Please explain in detail.

Newton said: (May 22, 2018)  
@Bindu.

Before the compiler reaches the print statement for 'A', badMethod () is called where an error is created and now the compiler will go and check for any error handling methods, so A won't be printed.

Matikana Rambabu said: (Jun 11, 2018)  
Can anyone explain why "D" is not printed? Please?

Sangeethar said: (Oct 2, 2018)  
I had compiled it & it shows as below.

CException in thread "main" java.lang.Error
at X.badMethod(X.java:22)
at X.main(X.java:7)

I had noted as if nothing got printed like"c".
Can anyone explain?

Aishwarya said: (Oct 25, 2018)  
D is also printed because after finally{ } block all statements are executed.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.