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 1 of 3.
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?
Anoms said:
8 years ago
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.
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.
(1)
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.
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.
Sangeethar said:
7 years ago
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?
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?
(1)
Dzingai said:
3 years ago
Hello @Sangeethar and @Murali.
The "C" is actually printed but it is concatenated with the word Exception as "CException in thread. Java. Lang. Error". This is because the coder used "print() " instead of "println() ".
The "C" is actually printed but it is concatenated with the word Exception as "CException in thread. Java. Lang. Error". This is because the coder used "print() " instead of "println() ".
Newton said:
7 years ago
@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.
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.
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();
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.
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