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.
Zaheer said:
1 year ago
When the main method is called, it first tries to execute the badMethod().
The badMethod() throws a RuntimeException.
Since the main method has a catch block for RuntimeException, it catches the exception and prints "B".
After the catch block, the final block is executed, which prints "D".
Finally, after the try-catch-finally block, "E" is printed. However, it is not displayed in the output because the program has already terminated due to the uncaught exception in the badMethod().
Therefore, the output of the program is "BD".
The badMethod() throws a RuntimeException.
Since the main method has a catch block for RuntimeException, it catches the exception and prints "B".
After the catch block, the final block is executed, which prints "D".
Finally, after the try-catch-finally block, "E" is printed. However, it is not displayed in the output because the program has already terminated due to the uncaught exception in the badMethod().
Therefore, the output of the program is "BD".
Soumyaranjan Barik said:
6 years ago
Before System.out.print("A").. one badMethod() is there, when flow will go to the try block badMethod() will throw *RuntimeException* , which must be catch by handler. So it will move to catch block//
catch (RuntimeException ex) /* Line 10 */
In line 10 its handled. So flow will forward next (it will print B) and one more Exception handled which has no use because No Exception is there.
So it will Execute Finally block (it will Print DE)
And the Answer is BDE.
catch (RuntimeException ex) /* Line 10 */
In line 10 its handled. So flow will forward next (it will print B) and one more Exception handled which has no use because No Exception is there.
So it will Execute Finally block (it will Print DE)
And the Answer is BDE.
Tushar S said:
8 years ago
If any program Exception occurs in try-block then 1) program abnormally terminated.2)Control comes out from program 3)JVM does not execute remaining try-block statement.
Control goes to Catch-block and Check any appropriate Exception class object created or not. if created then execute these Catch-block Statement.(above prog. object of RuntimeException() re)
After Executing Catch-block statement. Control does not go to try-block, but its control goes to finally block if present in the program.
After finally, block executing, control goes to the remaining statement (or logic).
In above prog, Inside static badMethod() method create the object of RuntimeException(), whenever call the method, then create the Exception i.e RuntimeException(), and goes control to catch-block and execute the statement and another block like finally.
Control goes to Catch-block and Check any appropriate Exception class object created or not. if created then execute these Catch-block Statement.(above prog. object of RuntimeException() re)
After Executing Catch-block statement. Control does not go to try-block, but its control goes to finally block if present in the program.
After finally, block executing, control goes to the remaining statement (or logic).
In above prog, Inside static badMethod() method create the object of RuntimeException(), whenever call the method, then create the Exception i.e RuntimeException(), and goes control to catch-block and execute the statement and another block like finally.
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?
Mounika said:
9 years ago
Can anyone clearly explain the entire program?
Parthipan said:
9 years ago
Hi, I'm not clear with the given explanation, can any help to to understand this?
Vikram said:
9 years ago
Define badMethod().
Gautam prusty said:
9 years ago
If any exception occurs in try block then control will go to the catch block.
try
{ badMethod();
System.out.print("A");
} here A is not printed,
If you have not understood this means ask me, I will help you.
try
{ badMethod();
System.out.print("A");
} here A is not printed,
If you have not understood this means ask me, I will help you.
Anusha said:
9 years ago
Can any explain clearly.
Vishnu said:
10 years ago
In exception handling at a time only one exception will caught, if there are more than one exception classes in catch block.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers