views:

179

answers:

7

I've written seven test cases for understanding the behavior of finally block. Can you guys explain the logic behind how finally works??

package core;

public class Test {
 public static void main(String[] args) {
  new Test().testFinally();
 }

 public void testFinally() {
  System.out.println("One = " + tryOne());
  System.out.println("Two = " + tryTwo());
  System.out.println("Three = " + tryThree());
  System.out.println("Four = " + tryFour());
  System.out.println("Five = " + tryFive());
  System.out.println("Six = " + trySix());
  System.out.println("Seven = " + trySeven());
 }

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

 protected String tryTwo() {
  String builder = "Cool";
  try {
   return builder += "Return";
  } finally {
   builder = null;
  }
 }

 protected int tryThree() {
  int builder = 99;
  try {
   return builder += 1;
  } finally {
   builder = 0;
  }
 }

 protected StringBuilder tryFour() {
  StringBuilder builder = new StringBuilder();
  try {
   builder.append("Cool");
   return builder.append("Return");
  } finally {
   builder.append("+1");
  }
 }

 protected int tryFive() {
  int count = 0;
  try {
   count = 99;
  } finally {
   count++;
  }
  return count;

 }

 protected int trySix() {
  int count = 0;
  try {
   count = 99;
  } finally {
   count = 1;
  }
  return count;

 }

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

Why builder = null is not working?

Why builder.append("+1") works where as count++( in trySeven()) does NOT work?

A: 

StringBuilder cannot be null it expects a string value.

Null arguments are generally bad when working with strings. count++ isn't declared??? builder.append("") you are appending a string - good. count=count++;

Joe Garrett
A `StringBuilder` reference certainly can be null. `count` is declared.
Matthew Flaschen
even after instantiation?
Joe Garrett
@Joe Garrett: Any time. `StringBuilder sb = new StringBuilder(); sb = null;` is legal code (even if it's not very useful in that instance). StringBuilder is a reference type, and *any* reference type can be null.
cHao
A: 

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.

Borealid
+10  A: 

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.

Matthew Flaschen
+2  A: 

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;
}
Robert Roland
welcome Robert ;)
HanuAthena
A: 

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.

bhups
A: 

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.

C Pirate
+1  A: 

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
   }
josefx