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 1 of 3.

Amit Kumar Singh said:   1 decade ago
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:   1 decade ago
/* 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:   1 decade ago
if(n&&(n-1)==0)
{
//power of 2
}
else
{
//not a power of 2
}

Sahil said:   1 decade ago
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:   1 decade ago
@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:   1 decade ago
Answer is C because Error class is not extends exception class it extends Throwable interface.

Ram said:   1 decade ago
The answer is B.

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

Steve said:   1 decade ago
Why does System.out.print("D"); not execute?

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


Post your comments here:

Your comments will be displayed after verification.