Skip to main content
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Does a finally block always get executed in Java?

Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is?

try {  
    something();  
    return success;  
}  
catch (Exception e) {   
    return failure;  
}  
finally {  
    System.out.println("I don't know if this will get printed out");
}

Answer*

Cancel
4
  • My apologies for commenting on such an old answer, but I don't think saying any software which for their proper behaviour depends on finally blocks always being executed after their try blocks complete are bugged. is quite right. finally blocks are usually used for resource clean-up to prevent leaks. How else would you do it? In the same vein, you cannot (or should not...) catch Errors either, as there is (usually) no reasonable way for a normal application to handle them.
    filpa
    –  filpa
    2021-06-09 09:35:05 +00:00
    Commented Jun 9, 2021 at 9:35
  • @user991710 If clean-up in the finally block is in example about closing an open file then not executing the finally block is not a problem, the OS closes it on program termination. If clean-up is deletion of a file that is a problem, the OS won't do it when the program is aborted before finally. If another part of the system depends on the java program always doing such deletion then the system in general and the java program in particular are bugged. You can't rely on finally always executing. You can only rely on the guarantee that finally has already been executed if NEXT has been executed
    Anonymous Coward
    –  Anonymous Coward
    2021-06-21 08:48:28 +00:00
    Commented Jun 21, 2021 at 8:48
  • Interesting take, although it forces some parts of your design as far as I can see. To expand on your deletion example: would you have a kind of 'reaper' task running that is responsible for deletion (clean-up) of files? I admit that, given your explanation, using finally may (given enough time, likely will) cause bugs, but is the additional development (and testing) time worth the potentially unnoticeable improvement in stability? More generally: does the amount of care that should go into a particular design depend on the subsystem in question? Time is a valuable resource, after all. :)
    filpa
    –  filpa
    2021-06-21 12:01:35 +00:00
    Commented Jun 21, 2021 at 12:01
  • 1
    @user991710 There is nothing wrong with using finally. You just need to know what it does and what it does not. For the particular case of deleting a file I would actually do it in the finally block. But being aware that doing so provides no guarantee (not even if the last line of the try block finished executing) I would take precautions to deal which such situation. So deleting previous leftover files as a first step of executing the program, if this is a service style program which would be re-run automatically. If system failing in such situation is of no concern then, sure, don't bother
    Anonymous Coward
    –  Anonymous Coward
    2021-06-22 19:44:02 +00:00
    Commented Jun 22, 2021 at 19:44

Morty Proxy This is a proxified and sanitized view of the page, visit original site.