• 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

Will "finally" block always run?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are situations that finally block will not run ?
Teach me ...
Thanks
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think when u do a system.exit() will not call the finally method since system.exit() will make the jvm to exit
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also the code in finally can raise an exception in which case the finally block may not complete.
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if the catch clause throws an Exception, finally will not execute.

What happens in the outer catch? Will finally clause execute?
What if no exception is thrown? Will the finally clause execute? In the outer try: .
The method tries to return a String and finally closes the connection.
Here is an odd-looking example:

What should the output be?
 
Tom Adams
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to test-out that "finally will not run if an exception is raised in the catch block". Does the following code test this statement? If so the output doesn't seem to support it...

---------------------------
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dang, you're right!
Thanks
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simon's Java 2 Cert Study Guide says the following:

The circumstances that can prevent execution of the code in a finally block are:

  • An exception arising in the finally block itself
  • The death of the thread
  • The use of System.exit()
  • Turning off the power to the CPU
  •  
    Garrett Smith
    Ranch Hand
    Posts: 401
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What about other types of errors like StackOverflowError or OutOfMemoryError?
    It seems (in my case) that a generic Error() (and by nature of inheritance, any subclass of Error) will cause the finally block not to execute. (is this the so-called "abnormal termination"?)


    Have to check my JLS now...

    Does turning off the power to the CPU really prevent a finally block from executing?
    [ February 10, 2003: Message edited by: Garrett Smith ]
     
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When I executed your example, the finally block was still executed:

    AFAIK, the only things that will prevent the finally block from executing are System.exit, turning the computer off, or possibly terminating the JVM from the OS.
    Even calling Thread.currentThread().stop() in the try block does not seem to prevent finally from executing.
     
    Garrett Smith
    Ranch Hand
    Posts: 401
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's hard to see the "Finally" text there. I didn't see it when I tried it.
     
    Garrett Smith
    Ranch Hand
    Posts: 401
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One more "evil" test:

    Eventually, an out of memory error occurs, the finally executes and prints "testtest.." (ad infinitum), and then the program terminates, printing "Exception in thread "main" java.lang.OutOfMemoryError.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic