• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Skip finally block

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is it possible to skip the 'finally' block even though its present after a try/catch block. i.e
try{
...//write some code here to make finally never execute.
}
finally
{
...
}
I know one such way, by using System.exit(1), inside try.
Is there any other way?
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you cannot. It may not even be working with the System.exit. Put
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.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. :-)

jan
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. :-)

jan



There are more... many more.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,

Would you be kind enough to tell me the other situations when these happen?

Regards
Vijay
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you could skip the finally block by writing an if statement directly within it:

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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Jagannathan:
Tony,

Would you be kind enough to tell me the other situations when these happen?

Regards
Vijay



Nope, I'm a mean nasty person
So instead, I'll only give you one:
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way is if the try block never completes.

Which can be achived by many ways, including deadlock, infinite loop and System.exit().
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/qb]<hr></blockquote>

tonysThread.interrupt();

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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think all "solutions" fit in one of three categories:
  • Something causes the thread to "stop" (wait/sleep/block) or loop for an infinite or indefinite amount of time, while the rest of the JVM continues to function
  • The JVM terminates - exit, control-C, power failure, nuclear armaggeddon, etc
  • An exception is thrown at the beginning of the finally block, causing the remainder of the finally block to be skipped. Execution will resume if & when another try/catch/finally handles the new exception

  • 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 ]
     
    Vijay Jagannathan
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the info guys.
    Its been really helpful.

    Cheers
    Vijay
     
    Ranch Hand
    Posts: 1078
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Using System.exit() doesn't skip anything, it merely terminates the entire JVM before the code is executed. If you consider the termination of a JVM before the execution of a finally block "skipping" the finally block then by that definition all code is skipped. It is impossible to skip a finally block, however there are a great many situations in which the finally block may not be executed before the JVM terminates. The question would be better phrased "Is it possible for code within a try/catch block to execute without the code in an associated finally block executing?" in which case the answer would most certainly be yes.
     
    Willie Smits can speak 40 languages. This tiny ad can speak only one:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic