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.
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.
Sonika said:
9 years ago
What is the relation between error and exception?
Sonika said:
9 years ago
Anyone exlain the hierarchy of exception handling for me please.
James said:
9 years ago
Why is it not printing "A" here?
Prithiv said:
9 years ago
I didn't get this, Can anyone explain this clearly?
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. ?
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers