Search code examples
javaexceptionfinally

What is the gist of finally block in Java?


I think on the following examples; but could not figure out what the importance of the finally block is. Can you tell me the difference of the executions of these two code samples? Also a real life example can be helpful.

Sample 1:

    try{
       // some code 1
    }catch(Exception ex){
       // print exception   
    }finally{
       // some code 2            
    }

Sample 2:

    try{
      // some code 1
    }catch(Exception ex){
      // print exception   
    }
    // some code 2

Solution

  • There is a big difference in the two snippets you've presented, e.g. when the catch block itself throws an exception, the finally block would still be executed by its semantics.

    That is the following snippet prints "Finally!", but not "What about me???":

        try {
            throw null;     // throws NullPointerException!
        } catch (Exception e) {
            int oops = 1/0; // throws ArithmeticException!
        } finally {
            System.out.println("Finally!"); // still gets executed!
        }
        System.out.println("What about me???"); // doesn't get executed!
    

    Generally speaking, the finally of a try block practically always gets executed. There's no such guarantee for any code following the try block.


    But what if my catch block is just a simple print statement?

    There's still no guarantee that it won't throw something. Something could still go wrong in e.g. the construction for the exception detailed message.

    Even if you make a best effort guarantee that the catch code is "safe" and the code following the try statement will always be executed, the question then becomes "Why?". Why avoid finally but then try so hard to replicate its semantics?

    finally semantics is guaranteed, requiring no burden of proof from either the writer or the reader of the code. Precisely because of this, it's idiomatic to use finally block to put mandatory "clean-up" code. Using finally guarantees correctness and enhance both writability and readability.