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 1 of 2.
Priyanka said:
1 decade ago
If exception is not caught then all the code after finally statement is run or not?
Laxmi said:
1 decade ago
Why not A got printed. Because after exception caught, program should follow natural flow?
Mohan said:
1 decade ago
I have also same question why A was not printed?
Tomnguyen said:
1 decade ago
After an exception has been caught, the rest of the code in try-catch block will not be executed, and other code outside the try-catch block will be executed. Note that the code inside finally block will run once any part of the code in try{} block has been run.
Chaithanya said:
1 decade ago
After method is called and exception is thrown, control transfers to catch block and never returns to try block so sop is not executed in try block
Exception is handled but sop(b) is not in output.
@Tomnguyen. You said it won't execute in catch block too...what about System.out.println(e) which returns exception when used in catch block?
Exception is handled but sop(b) is not in output.
@Tomnguyen. You said it won't execute in catch block too...what about System.out.println(e) which returns exception when used in catch block?
Sai said:
1 decade ago
Hi Mohan,
Before System.out.print("A").. one badMethod() is there. when compiler found an exception, further codes wont be execute.thats why A is nt print. it will execute if that exception will be caught by catch block. So this RuntimeException is caught in catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
Thats why b is printed. and finally block will always printed. and the rest of the stmt also printed because the exception has already been caught. I hope you understood.
Before System.out.print("A").. one badMethod() is there. when compiler found an exception, further codes wont be execute.thats why A is nt print. it will execute if that exception will be caught by catch block. So this RuntimeException is caught in catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
Thats why b is printed. and finally block will always printed. and the rest of the stmt also printed because the exception has already been caught. I hope you understood.
Mukesh said:
1 decade ago
Thank you @Sai,
But I have one more doubt why it not prints "C". An RuntimeException is caught but it should also be caught in super class java.lang.Exception .
I am waiting for your reply.
But I have one more doubt why it not prints "C". An RuntimeException is caught but it should also be caught in super class java.lang.Exception .
I am waiting for your reply.
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.
Avdhesh Gupta said:
1 decade ago
When I write throw new Exception() I have to add to the method throws but when I throw new Runtime Exception() I don't have to mention throws, Why?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers