File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Query reg. try catch finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Query reg. try catch finally" Watch "Query reg. try catch finally" New topic

Query reg. try catch finally

Manish Jaju

Joined: Jul 21, 2004
Posts: 24
In a program, if it uses finally block, it will be executed. Is there any possiblity so that it(finally block) should not get executed in any condition. Please clarify.
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
Well... the main goal of a finally statement is that we are almost sure that it will be execute if exceptions are throw. If you don't want it to be executed just don't put a finally statement... or use a boolean in your finally

boolean doFinally = true;


}catch (Throwable e){
doFinnaly = false;
if (doFinally){

But this is stupid code...

Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
As far as I know, the only regular way to have a finally block not executed is to call System.exit(x) in the try- or catch-block.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Query reg. try catch finally
It's not a secret anymore!