aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
When will the code after finally block gets executed... In some cases it does not get executed and some times gets executed.... Plz resolve this with examples if u can........


$Hi/\bEr
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
Ex: if you have a return statement in try or catch block , the finally block will be executed and the control goes to the calling method.
In that case code after finally will not be executed,else it will be executed.
Ex:
try{
return "something";---control goes back to the calling method
}
finally{
System.out.println("finally block");
}
............/*after finally code--in this example this code will not be
executed.*/
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Thanks for your reply... If u can explain other cases which dont have return stmt and all.....
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
If we do not have return statement then the code below finally will be executed(of course if we do not have return statement in finally itself)
Read K&B , it is really good
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Check it Out..
try{ //throws some eccpn }
catch(Excpn e){}
finally{}
//Some Code after finally...


My Queries...
if there occurs excpn and wat happens...
if there is no catch block and there if occurs excpn wat happens....
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
If there is exception , the catch block catches and handles the exception.
If there were no catch block, the finally block is executed and the exception is thrown.

Note that finally block executes after the exception is thrown but before the exception is executed if there is no catch block.
Ramasubbhu Allur Kuppusamy
Ranch Hand

Joined: Sep 16, 2005
Posts: 120
Shiaber,
Finally block will be executed, no matter what!


Regards,<br />Ram.<br />SCJP 1.4
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
i have confusions whether the code after finally block will execute or not... in some book i read that if the excpn is not handled(ie no catch block)... the finally will get executed and the pgm terminates without executing the code after finally block....

Am i right....
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Calling System.exit() an prevent a finally block from executing. It will shut down the virtual machine.
Anupama Ponnapalli
Ranch Hand

Joined: Jun 12, 2006
Posts: 66
Originally posted by Shiaber M:
in some book i read that if the excpn is not handled(ie no catch block)... the finally will get executed and the pgm terminates without executing the code after finally block....

Am i right....


Thats correct, if an exception is not caught, the finally block will execute (since its guaranteed to). but, the code in the method after the finally block will not be executed.
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Code after Finally block executes when
When program runs without any exceptions.
When the exception is caught in any of the catch.
When there is no return statement in try or catch or finally.
When the exceptions are not rethrown.
When System.exit() is called
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Well said... Thaynesh..........
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
But ...When System.exit() is called, I think it wont get executed....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception
 
Similar Threads
Servlet Concepts
static block
Thread start() and run() help
Calling init
Confirmation Required(About Filter Sequence)