Java Programming - Exceptions - Discussion

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 */
    } 
}

[A]. finally
[B]. exception finished
[C]. finally exception finished
[D]. Compilation fails

Answer: Option C

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).


Bhushan said: (Jul 16, 2010)  
But every try block must followecd by catch block. ?

Sankyrahul said: (Jun 6, 2011)  
Execution of finally must be followed by execution all catch block. ?

Sankyrahul said: (Jun 6, 2011)  
The exception cannot be assigned to the parameter of any catch clause of the try statement ..why?

Manish said: (Aug 16, 2011)  
When you throw exception the main function should also have throw.

Jagadeeshwar said: (Nov 8, 2012)  
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.

Jim Bob said: (Jun 20, 2013)  
I actually put this in netbeans and ran it under jdk 1.7 and got "exception finished".

Swas_99 said: (Aug 2, 2013)  
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.

Rekha said: (Oct 29, 2013)  
Please explain meaning of the statement "throw new Exception()" ?

Bisan said: (Nov 11, 2014)  
I tried this code in Netbeans IDE under jdk1.8.0_11, the output is:

Finally exception finished.

Shyam said: (Dec 16, 2014)  
amethod() is valid or not because some program it shows the error.

Stu said: (Mar 26, 2015)  
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?

Amit said: (Feb 23, 2016)  
Output will be only "finally exception" even though excp is catched program not continue.

Aarti Gupta said: (Mar 1, 2016)  
I don't get it. First exception must be catched. And then it should run finally statement.

Richards said: (Jul 28, 2016)  
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.

Richards said: (Jul 28, 2016)  
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.

Renjith said: (May 3, 2017)  
The exception cannot be assigned to the parameter of any catch clause of the try statement. Why? please give me the correct answer.

Saswat said: (Jul 26, 2017)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.