• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does a finally block always run?  RSS feed

 
Sateesh Kumar Allu
Greenhorn
Posts: 9
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any possible cases to stop the execution of the finally() ..
when does the finally block not execute while try or catch block is interrupted.....
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one edge case that I know of. If you call System.exit() the JVM will exit immediately and the finally block will not run.

I suspect if the JVM abruptly crashes there would also be no way for any finally block to run.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find a fuller list of circumstances in which a finally block doesn't run in this thread, or here. The definitive answer is in the Java® Language Specification, but that can be difficult to read.
 
Sateesh Kumar Allu
Greenhorn
Posts: 9
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir as per you, When i stop the try catch threads finally() executing...How it is possible
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm.... I think we need a better English translation of your question. I have no idea what that sentence was supposed to mean or what your question is. Sorry, but could you try to restate your question more clearly?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tht is how the language is programmed. Write a little class with one method containing a try‑catch‑finally, and view that class with
javac Foo.java
javap -c Foo

That will give you a printout explaining the bytecode.You will notice that the try ends with a goto or jump instruction, and all the catch clauses also end with a jump. They will all point to the same number, which is where the finally begins.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sateesh Kumar Allu wrote:. . . When i stop the try catch threads finally() executing...How it is possible
You do not stop any of those threads.

Look at this codeYou MUST NOT use Thread#stop in real life.Note that the stop call does not prevent the finally clause from being executed. I think the instructions to go to the finally are incorporated in the Exception table: the finally begins on line 90.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sateesh Kumar Allu wrote:When i stop the try catch threads finally() executing...How it is possible

I think perhaps another question is: Why do want (or think you need) to know this?

The whole point of finally() is that - other than the extreme circumstances listed by others - it always executes. That's the reason it was created.

It seems to me that you're worrying about stuff that you really don't need to at the moment, and it's likely to distract you.

Use finally() the way your books (or teacher) tells you to. There'll be plenty of time later on to work out all the possible occasions it might not run - and most of them (other than Ole Kristian's example) are likely to be things you have absolutely no control over.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!