Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Finding the output (Q.No. 1)
1.
What will be the output of the program?
public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "Finally" ); 
        } 
    } 
}
Finally
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
Answer: Option
Explanation:
If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
  1. An exception arising in the finally block itself.
  2. The death of the thread.
  3. The use of System.exit()
  4. Turning off the power to the CPU.

I suppose the last three could be classified as VM shutdown.

Discussion:
25 comments Page 2 of 3.

Tamilkumaran said:   8 years ago
Thanks for all your informations.

I got more information from your conversations.

Priyal said:   8 years ago
Nice explanation, I got more information from your conversation thanks a lot.

Deepak pathak said:   1 decade ago
Catch file is not define then this function is not present the finally.

Sai Kumar said:   1 decade ago
return; means it's returning nothing.
So we can just give return;

Pramod said:   1 decade ago
Thanks, What Difference System.exit(0), And System.exit(1)?

Rahul Nick said:   8 years ago
Thank you friends for giving this valuable knowledge.

Sonu raj said:   1 decade ago
If method type is void then how it is returning ?

K.sindhu said:   8 years ago
Yeah, very nice explanation. Thanks a lot.

ImdaduL Haque said:   7 years ago
I can't understand it. How can it work?

Suraj Dhaigude said:   5 years ago
Thanks all for giving the explanation.


Post your comments here:

Your comments will be displayed after verification.