Java Programming - Exceptions - Discussion
Discussion Forum : Exceptions - Finding the output (Q.No. 6)
6.
What will be the output of the program?
public class Test
{
public static void aMethod() throws Exception
{
try /* Line 5 */
{
throw new Exception(); /* Line 7 */
}
finally /* Line 9 */
{
System.out.print("finally "); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}
Answer: Option
Explanation:
This is what happens:
(1) The execution of the try block (line 5) completes abruptly because of the throw statement (line 7).
(2) The exception cannot be assigned to the parameter of any catch clause of the try statement therefore the finally block is executed (line 9) and "finally" is output (line 11).
(3) The finally block completes normally, and then the try statement completes abruptly because of the throw statement (line 7).
(4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints "exception".
(5) Lastly program execution continues, because the exception has been caught, and "finished" is output (line 24).
Discussion:
18 comments Page 1 of 2.
Richards said:
9 years ago
Explanation:
1. amethod() is called,
2. Try block is executed.
3. Throws exception.
4. Since it is followed by finally first the finally is printed.
5. Then the catch statement matching for tat try exception throw line is found.
6. Finally, catch block executes.
1. amethod() is called,
2. Try block is executed.
3. Throws exception.
4. Since it is followed by finally first the finally is printed.
5. Then the catch statement matching for tat try exception throw line is found.
6. Finally, catch block executes.
(1)
Shyam said:
1 decade ago
amethod() is valid or not because some program it shows the error.
Sun said:
8 years ago
Thanks. @Richard.
Saswat said:
8 years ago
main() call aMethod.
aMethod() throw Exception. So it gives chance to caller method to handle it.
So, 1st finally is printed.
Then it found appropriate catch block. So exception printed.
Then finished printed.
aMethod() throw Exception. So it gives chance to caller method to handle it.
So, 1st finally is printed.
Then it found appropriate catch block. So exception printed.
Then finished printed.
Renjith said:
8 years ago
The exception cannot be assigned to the parameter of any catch clause of the try statement. Why? please give me the correct answer.
Richards said:
9 years ago
I just run this program and got the o/p/ as;
"Finally exception finished "
How a try block outside the main function will run first? I am amazed.
"Finally exception finished "
How a try block outside the main function will run first? I am amazed.
Aarti gupta said:
9 years ago
I don't get it. First exception must be catched. And then it should run finally statement.
Amit said:
10 years ago
Output will be only "finally exception" even though excp is catched program not continue.
Stu said:
1 decade ago
Exception is a checked exception, but why the main method don't have the throws exception because it calls a method that can throw a checked exception?
Bhushan said:
2 decades ago
But every try block must followecd by catch block. ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers