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 2 of 3.
Murali said:
8 years ago
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.
And I got output only error message and "C" is not printed.
Can anyone explain the flow?
Thank you.
Vijay said:
8 years ago
I couldn't get it, can someone help me out?
Ammu said:
9 years ago
Can't understand. So please explain me clearly.
Sachith said:
9 years ago
Can anyone explain why this line not execute "System.out. print("D") ;"?
Suma said:
9 years ago
@Sonika.
Throwable is the parent class, exception and error are subclass of throwable.
Throwable is the parent class, exception and error are subclass of throwable.
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.
Sonika said:
9 years ago
Anyone exlain the hierarchy of exception handling for me please.
Prithiv said:
9 years ago
I didn't get this, Can anyone explain this clearly?
James said:
9 years ago
Why is it not printing "A" here?
Hritika said:
10 years ago
Isn't is mandatory to use throws keyword with bad method () if throw keyword is used in the method definition. ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers