• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

finally block

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any other possible cases where the finally block is not executed besides a call to System.exit() or hardware failure?
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Yi Meng
Are there any other possible cases where the finally block is not executed besides a call to System.exit() or hardware failure?
Actually,finally block always executes,except for the System.exit().
But you can try the idea that the try, catch and the finally block place inside the loop such as for loop

You can use it to check whether the finally block is executed.
I without use the compiler to test it.
you can help me to check it
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will still be executed.........
i remebered there is actually one more case(or more) that finally is NOT executed, it has sth to do with return.....but just forgot exact info....
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally still executes if return is called within a try clause.
There is a tricky part though. If finally modify the value that return gives back, the returned value is the original of return, not the modified one by finally. This so because before executing the jump to finally the value of return is stored. Then finally executes and the flow of comtrol goes back to execute return. But before of this, return restores the value previously saved.

prints
1
0
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More about finally and return:

it will print 2 instead of 1.........
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also read a suprising thing in my book that finally is executed even if OutOfMemory error is thrown. I figured, well, if the program runs out of memory there probably is no guarantee that the program will run normally, but that's what the book says.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In bill brogden's exam latest book exam cram 2 310-035, a similar type of question had a return in the try and a return in the finally,
The return value of the finally was listed as the right answer... not the one in the try....Is the exam answer wrong?

What about if there was a return in the catch vs return in the finally? Please clarify
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
preeti, the return in the finally clause will prevail because the return in the catch or try will be executed only if finally clause completes. Only when the finally clause completes the execution resumes at the point of the instruction that caused the jump out of the try/catch clauses.
More clearly: an instruction that will cause to leave the try/catch clauses --say a return, throw, break or continue-- will force finally to be executed. After that the instruction itself is completed, but only if finally ends. If there another jumping-out-instruction in finally it gets executed and the flow of control will not go back.
Think of finally as a minisubroutine.
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jose, that was a very good explanation.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!