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 */
}
}
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.
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();
And what is the meaning by this that Exception is not a super class of error();
Steve said:
1 decade ago
Why does System.out.print("D"); not execute?
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.
Compilation fail because it complain to required Throwable instead of Exception in catch block. TRY IT.
Akhilesh said:
1 decade ago
Answer is C because Error class is not extends exception class it extends Throwable interface.
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.
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.
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?
{
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?
Kleen said:
1 decade ago
if(n&&(n-1)==0)
{
//power of 2
}
else
{
//not a power of 2
}
{
//power of 2
}
else
{
//not a power of 2
}
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers