• 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

Regarding finnaly

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

This is sirisha, I have a Doubt On Finally Block

In How May Ways We Can Scip Finally Block I know one way: By returning value in try and Catch Block.

Can One Can Tell Me Another ways to Scip Finally Block.other than above.

Waiting .....

Thanks && Regards,
Sirisha
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sirisha makkapati:
In How May Ways We Can Scip Finally Block I know one way: By returning value in try and Catch Block.


No, you are wrong - returning a value in the try or catch block does not skip the finally block.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "finally" block will always run, in ordinary code. All the ways of skipping it are dubious, to various extents.

The least bad way is to exit the whole Java Virtual Machine using System.exit(). This is not deprecated, but code that uses System.exit() may be difficult to re-use. Inappropriate use of System.exit() in applications that write to files, network or database may cause corruption.

Even worse ways to skip "finally" would involve using deprecated methods like Thread.stop(), evil operations using native code via JNI, provoking JVM crashes, killing the Java process via an external system tool etc.

Basically, avoid doing any of the above and you can then, in all but the most sensitive applications, assume "finally" will always run.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In How May Ways We Can Scip Finally Block



Why would you have a finally block if you want that to be skipped.
 
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 finally block will always run when the try block completes normally or abruptly.

Only way to not run finally is to make the try block not complete at all by using infinite loop, infinite wait, System.exit(), ... Thread.stop() does not work since it uses an exception (ThreadDeath by default) to stop the Thread and that exception is handled as any other exception.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, Thread.stop is depracated:
http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

A simple way to have some control if the finally is executed or not - its content, at least - is to use a boolean control variable:
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic