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 */
    } 
}
finally
exception finished
finally exception finished
Compilation fails
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.

Swas_99 said:   1 decade ago
Jim is right.

The output should be "exception finished" .

When an exception is thrown within a try block, a matching catch block is searched up the hierarchy(as long as code lies within try{}) -> first in the current method, then the calling method .. and so on. If no catch block is found till the last calling method = main(), control is transferred to OS to handle it appropriately.

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)

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.

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.

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?

Jagadeeshwar said:   1 decade ago
Try block must be followed by either catch block or finally or both. But it must be followed by atleast catch block or finally block.

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.

Sankyrahul said:   1 decade ago
The exception cannot be assigned to the parameter of any catch clause of the try statement ..why?

Bisan said:   1 decade ago
I tried this code in Netbeans IDE under jdk1.8.0_11, the output is:

Finally exception finished.

Aarti gupta said:   9 years ago
I don't get it. First exception must be catched. And then it should run finally statement.


Post your comments here:

Your comments will be displayed after verification.