a couple of print lines in the finally and try it. The definition of
a finally block is to always be executed. If, there is something you
do not want done, don't put it in a finally block, or place it inside an
if inside the finally block using a boolean flag that will get set in the
try and/or catch block.
Originally posted by Jan Groth:
a slight variation of this question:
"in which situations a finally block is _not_ executed?"
(1) a call to System.exit
(2) an endless loop before
no more situations.
you don't want anything in your finally block which you don't want to execute. :-)
There are more... many more.
As long as the variables/objects that you are testing as part of the condition are visible / in scope, this will achieve the desired results.
Its a bit messy though. I'm not sure its that elegant, and I guess the purists would frown upon it . It would work though.
[ May 23, 2006: Message edited by: Nathan Russell ]
My point being that this -- and any number of other "solutions" -- isn't actually skipping the finally at all; just delaying -- perhaps for an infinite time -- reaching it. There really is no way to skip it (unless you want to count System.exit() -- even that is cheating, really, as you're only skipping it by terminating the program. Truly "skipping it" would mean continuing the same thread in the same program after the try block completes, and that's not possible.
[ May 24, 2006: Message edited by: Ernest Friedman-Hill ]
None of these really skip the finally as EFH points out, though the last may come close in skipping part of the block. It's important for people to realize that there's no guarantee that the entire finally block will be executed - only the beginning. (Assuming no thread stop or JVM termination.)
[ May 24, 2006: Message edited by: Jim Yingst ]