Understanding the 'finally' block

后端 未结 6 962
误落风尘
误落风尘 2021-01-13 04:23

I\'ve written seven test cases for understanding the behavior of the finally block. What is the logic behind how finally works?

pac         


        
相关标签:
6条回答
  • 2021-01-13 04:30

    builder = null and builder.append("+1") are working. It's just that they're not affecting what you're returning. The function returns what the return statement has, regardless of what happens afterward.

    The reason there is a difference is because builder is passed by reference. builder=null changes the local copy of builder. builder.append("+1") affects the copy held by the parent.

    0 讨论(0)
  • 2021-01-13 04:35

    Let's start with use case you'll see more often - you have a resource that you must close to avoid a leak.

    public void deleteRows(Connection conn) throws SQLException {
        Statement statement = conn.createStatement();
        try {
            statement.execute("DELETE * FROM foo");
        } finally {
            statement.close();
        }
    }
    

    In this case, we have to close the statement when we're done, so we don't leak database resources. This will ensure that in the case of an Exception being thrown, we will always close our Statement before the function exits.

    try { ... } finally { ... } blocks are meant for ensuring that something will always execute when the method terminates. It's most useful for Exception cases. If you find yourself doing something like this:

    public String thisShouldBeRefactored(List<String> foo) {
        try { 
            if(foo == null) {
                return null;
            } else if(foo.length == 1) {
                return foo.get(0);
            } else {
                return foo.get(1);
            }
        } finally {
            System.out.println("Exiting function!");
        }
    }
    

    You're not really using finally properly. There is a performance penalty to this. Stick to using it when you have Exception cases that you must clean up from. Try refactoring the above to this:

    public String thisShouldBeRefactored(List<String> foo) {
        final String result;
    
        if(foo == null) {
            result = null;
        } else if(foo.length == 1) {
            result = foo.get(0);
        } else {
            result = foo.get(1);
        }
    
        System.out.println("Exiting function!");
    
        return result;
    }
    
    0 讨论(0)
  • 2021-01-13 04:44

    Once you do the return, the only way to override that is to do another return (as discussed at Returning from a finally block in Java, this is almost always a bad idea), or otherwise complete abruptly. Your tests don't ever return from a finally.

    JLS §14.1 defines abrupt completion. One of the abrupt completion types is a return. The try blocks in 1,2,3,4, and 7 abruptly complete due to returns. As explained by §14.20.2, if the try block completes abruptly for a reason R besides a throw, the finally block is immediately executed.

    If the finally block completes normally (which implies no return, among other things), "the try statement completes abruptly for reason R.". In other words, the return initiated by the try is left intact; this applies to all your tests. If you return from the finally, "the try statement completes abruptly for reason S (and reason R is discarded)." (S here being the new overriding return).

    So in tryOne, if you did:

    finally {
                builder = null;
                return builder;
            }
    

    this new return S would override the original return R.

    For builder.append("+1") in tryFour, keep in mind StringBuilder is mutable, so you're still returning a reference to the same object specified in the try. You're just doing a last minute mutation.

    tryFive and trySix are straight-forward. Since there is no return in the try, the try and finally both complete normally, and it executes the same as if there was no try-finally.

    0 讨论(0)
  • 2021-01-13 04:44

    Consider what the compiler is actually doing for the return statement, for instance in tryOne(): it copies a reference to builder back to the calling function's environment. After it's done this, but before control goes back to the calling function, the finally block executes. So you have something more like this, in practice:

    protected StringBuilder tryOne() {
        StringBuilder builder = new StringBuilder();
        try {
            builder.append("Cool");
            builder.append("Return");
            StringBuilder temp = builder;
            return temp;
        } finally {
            builder = null;
        }
    }
    

    Or, in terms of the order that statements actually get executed (ignoring possible exceptions, of course), it looks more like this:

    protected StringBuilder tryOne() {
        StringBuilder builder = new StringBuilder();
        builder.append("Cool");
        builder.append("Return");
        StringBuilder temp = builder;
        builder = null;
        return temp;
    }
    

    So setting builder = null does run, it just doesn't do anything useful. However, running builder.append("something") will have a visible effect, since both temp and builder refer to the same (mutable) object.

    Likewise, what's really happening in trySeven() is something more like this:

    protected int trySeven() {
        int count = 0;
        count = 99;
        int temp = count;
        count++;
        return temp;
    }
    

    In this case, since we're dealing with an int, the copies are independent, so incrementing one doesn't affect the other.

    All that said, the fact remains that putting return statements in a try-finally block is quite clearly confusing, so if you've got any kind of choice in the matter, you'd be better off rewriting things so that all your return statements are outside any try-finally blocks.

    0 讨论(0)
  • 2021-01-13 04:46

    Why builder = null is not working?
    Because you are setting the local reference to null which will not change the content of the memory. So it is working, if you try to access the builder after finally block then you'll get null.
    Why builder.append("+1") work?
    Because you are modifying the content of the memory using the reference,that's why it should work.
    Why count++ does not work in testFive()?
    It is working fine with me. It outputs 100 as expected.

    0 讨论(0)
  • 2021-01-13 04:48

    The finally block is executed when you leave the try block. The "return" statement does two things, one it sets the return value of the function and two it exits the function. Normally this would look like an atomic operation but within a try block it will cause the finally block to execute after the return value was set and before the function exits.

    Return execution:

    1. Assign return value
    2. run finally blocks
    3. exit function

    Example one (primitive):

    int count = 1;//Assign local primitive count to 1
    try{
      return count; //Assign primitive return value to count (1)
    }finally{
      count++ //Updates count but not return value
    }
    

    Example two(reference):

    StringBuilder sb = new StringBuilder();//Assign sb a new StringBuilder
    try{
        return sb;//return a reference to StringBuilder
    }finally{
        sb.append("hello");//modifies the returned StringBuilder
    }
    

    Example three (reference):

       StringBuilder sb = new StringBuilder();//Assign sb a new StringBuilder
       try{
          return sb;//return a reference to StringBuilder
       }finally{
          sb = null;//Update local reference sb not return value
       }
    

    Example four (return):

       int count = 1;   //assign count
       try{
          return count; //return current value of count (1)
       }finally{
          count++;      //update count to two but not return value
          return count; //return current value of count (2) 
                        //replaces old return value and exits the finally block
       }
    
    0 讨论(0)
提交回复
热议问题