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.

Parul said:   1 decade ago
The main method line is wrongly written (should be public static void main (String args[]) ) then how the code will be executed. No output will come.

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

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

Dheeraj Singh said:   10 years ago
System.exit(0) means process terminate abruptly and System.exit(1) means process terminate successfully.

N.saikrishna said:   9 years ago
Speciality of finally block is it will be executed irrespective of the exception is raised or not raised.

BhargavDave said:   9 years ago
What do for generate an exception?

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

I got more information from your conversations.

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

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

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


Post your comments here:

Your comments will be displayed after verification.