aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Will Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Will "finally" block always run?" Watch "Will "finally" block always run?" New topic
Author

Will "finally" block always run?

Jimmy Chiu
Ranch Hand

Joined: Feb 08, 2003
Posts: 58
What are situations that finally block will not run ?
Teach me ...
Thanks


go...
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
I think when u do a system.exit() will not call the finally method since system.exit() will make the jvm to exit
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
Also the code in finally can raise an exception in which case the finally block may not complete.


Tom
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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?


comp.lang.javascript FAQ: http://jibbering.com/faq/
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
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

Joined: Jun 27, 2002
Posts: 401
Dang, you're right!
Thanks
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
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


  • Java has reached Brazil =D
    Garrett Smith
    Ranch Hand

    Joined: Jun 27, 2002
    Posts: 401
    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 ]
    John Paverd
    Ranch Hand

    Joined: Nov 17, 2002
    Posts: 115
    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.


    SCJP 1.4
    Garrett Smith
    Ranch Hand

    Joined: Jun 27, 2002
    Posts: 401
    It's hard to see the "Finally" text there. I didn't see it when I tried it.
    Garrett Smith
    Ranch Hand

    Joined: Jun 27, 2002
    Posts: 401
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Will "finally" block always run?
     
    Similar Threads
    Return statement in try block
    GUI update.
    System.exit(0) &System.exit(1)
    When finally block does not run?
    No Current Row in the ResultSet when Migrating from SQL 2000 to 2005