• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding finnaly  RSS feed

 
sirisha makkapati
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • 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.
 
Luciano Mantuaneli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!