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 3 of 3.

Praveen.v said:   1 decade ago
A try can exist with out any catch block but catch can not exit with out try block.

Praveen.vaka said:   1 decade ago
Try can exist with out any catch.

Jyoti said:   1 decade ago
Thanx for this info..

Jafar said:   1 decade ago
How the code works?

Muthu kumar said:   1 decade ago
How it will runs without catch ?


Post your comments here:

Your comments will be displayed after verification.