Java Programming - Exceptions - Discussion
Discussion Forum : Exceptions - Finding the output (Q.No. 4)
4.
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}
Answer: Option
Explanation:
A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.
Discussion:
20 comments Page 2 of 2.
Rasika said:
1 decade ago
The options mentioned are wrong!
Runtime Exception is subclass of Exception so above piece of code will not compile.
Runtime Exception is subclass of Exception so above piece of code will not compile.
Ayushisa36491 said:
9 years ago
Line 10 has been declared as a comment, but then also exception is caught, how that suppose to take place?
Laxmi said:
1 decade ago
Why not A got printed. Because after exception caught, program should follow natural flow?
Aamir said:
1 decade ago
Hi @Mukesh.
If the Exception is caught then the catch try and catch block no compile.
If the Exception is caught then the catch try and catch block no compile.
Priyanka said:
1 decade ago
If exception is not caught then all the code after finally statement is run or not?
Parthipan said:
9 years ago
Hi, I'm not clear with the given explanation, can any help to to understand this?
Mohan said:
1 decade ago
I have also same question why A was not printed?
Mounika said:
9 years ago
Can anyone clearly explain the entire program?
Anusha said:
9 years ago
Can any explain clearly.
Vikram said:
9 years ago
Define badMethod().
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers