• 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

finally always executed - KAM

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer KAM's Guide to Java Certification:
Sec 5.6 p156
The code in the finally block is always executed.
p159
If any code in the try block is executed, then the finally block is guaranteed to be executed, regardless of whether any catch block is executed.
Example 5.11

I inserted System.exit(1) and finally did not get executed in either instances (other is commented as //~JB). In what context is KAM referring to by his statements, anyone?
------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
[This message has been edited by James Baud (edited January 11, 2001).]
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,
Reading the API for System.exit(), I ran into this statement, This method calls the exit method in class Runtime. This method never returns normally.. Since this method never returns "normally", I'm assuming your finally {} block never gets executed.
With the System.exit(), the execution never returns from your method DivisionByZero3(). I'm pretty sure KAM was referring to the case where the execution of the method does return, in this case the finally {} block would get executed.
Why do you care if the finally {} block gets executed if you call System.exit()?
-Peter
 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
Thanks for your response. As a Java newbie, I'm accepting KAM's statement as a matter of blind faith because he is, in my point of view, the expert in the JAVA domain. My acceptance of his statement carries with it the belief that under all cirumstances that statement is true. In this case, there is a "qualification" where it is not true all the time. As you pointed out, the premise that the finally {} block is executed all the time is only true when one does not call within it a method that never returns normally
Whether I care or not that finally {} block gets executed if I call System.exit() is immaterial. What's relevant is if I did not know the context where a certain premise applied, I might, in the future be faced with, let's say a critical situation where the design of my whole application is dependent on some concept being true all the time, and then the unthinkable happens...
------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The system.exit(1) command is a forcefull way of exiting from the program. This does not care to run the finally block. Hope the explanation given Peter should solve ur doubts.
Good Luck !!!
reply
    Bookmark Topic Watch Topic
  • New Topic