Java Programming - Exceptions - Discussion
Discussion Forum : Exceptions - Finding the output (Q.No. 8)
8.
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}
Answer: Option
Explanation:
(1) A RuntimeException is thrown, this is a subclass of exception.
(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.
(3) The exception is caught (line 10) and "B" is output (line 12)
(4) The finally block (line 14) is always executed and "C" is output (line 16).
(5) The exception was caught, so the program continues with line 18 and outputs "D".
Discussion:
2 comments Page 1 of 1.
Prabhat said:
1 decade ago
But when a method is capable of throwing some exception and cannot catch it then throws statement should be there with exception type.
Sanjj said:
8 years ago
badMethod() should throws RuntimeException right?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers