Java Programming - Exceptions - Discussion


What will be the output of the program?

public class Foo 
    public static void main(String[] args) 
            System.out.println( "Finally" ); 

[A]. Finally
[B]. Compilation fails.
[C]. The code runs with no output.
[D]. An exception is thrown at runtime.

Answer: Option A


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.

Muthu Kumar said: (Jul 3, 2011)  
How it will runs without catch ?

Jafar said: (Oct 25, 2011)  
How the code works?

Jyoti said: (Nov 10, 2011)  
Thanx for this info..

Praveen.vaka said: (Nov 28, 2011)  
Try can exist with out any catch.

Praveen.v said: (Nov 28, 2011)  
A try can exist with out any catch block but catch can not exit with out try block.

Sonu Raj said: (Jun 22, 2012)  
If method type is void then how it is returning ?

Sai Kumar said: (Sep 7, 2012)  
return; means it's returning nothing.
So we can just give return;

R.VINOD KUMAR said: (Oct 17, 2012)  
Finally block is executing means releasing the resources used.

So always resources must be released after using, So Finally block must be executed compulsorly everytime.

If exception raises in try block, then JVM searches for Particular catch block, if catch block is absent then Finally will be executed to close the resources and then error/exception message will be printed on command prompt.

But here in this case no exception raised in try block. So, everything is fine (means simply Finally executed).

Av.nikhil said: (May 8, 2013)  
The "return;" statement in try block has to send the control back to the calling method (main) without returning any value (because it allowed only in void methods). So without searching for catch blocks control going to the calling method.

But anyhow finally block should execute before the control goes the main method.

Akhilesh said: (Oct 14, 2013)  
If we use finally block it always executes anyhow to complete unfinished tasks. So here Finally will be printed on the output screen as a result.

Parul said: (Nov 9, 2013)  
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: (Feb 15, 2014)  
Catch file is not define then this function is not present the finally.

Pramod said: (Aug 3, 2014)  
Thanks, What Difference System.exit(0), And System.exit(1)?

Dheeraj Singh said: (Oct 2, 2015)  
System.exit(0) means process terminate abruptly and System.exit(1) means process terminate successfully.

N.saikrishna said: (Sep 18, 2016)  
Speciality of finally block is it will be executed irrespective of the exception is raised or not raised.

BhargavDave said: (Dec 6, 2016)  
What do for generate an exception?

Tamilkumaran said: (Jun 8, 2017)  
Thanks for all your informations.

I got more information from your conversations.

K.sindhu said: (Jun 15, 2017)  
Yeah, very nice explanation. Thanks a lot.

Priyal said: (Sep 19, 2017)  
Nice explanation, I got more information from your conversation thanks a lot.

Rahul Nick said: (Oct 28, 2017)  
Thank you friends for giving this valuable knowledge.

Bhanu said: (Mar 1, 2018)  
Hi Parul,

Exchanging the place or syntax of the main method will not be affected for execution of program except void must be before main.

ImdaduL Haque said: (Apr 1, 2018)  
I can't understand it. How can it work?

P.vyshnavi said: (Jul 21, 2018)  
I think compilation fails because every try block having catch block is there but in this program, catch block is not there.

Correct me, if I am wrong.

Suraj Dhaigude said: (May 11, 2020)  
In the try block, no any exception so no need to catch block control direct finally method so it's print finally.

Suraj Dhaigude said: (May 11, 2020)  
Thanks all for giving the explanation.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.