Try this piece of code. Why does getValueB() return 1 instead of 2? After all, the increment() function is getting called twice.
public class ReturningFromFinally
{
public static int getValueA() // This returns 2 as expected
{
try { return 1; }
finally { return 2; }
}
public static int getValueB() // I expect this to return 2, but it returns 1
{
try { return increment(); }
finally { increment(); }
}
static int counter = 0;
static int increment()
{
counter ++;
return counter;
}
public static void main(String[] args)
{
System.out.println(getValueA()); // prints 2 as expected
System.out.println(getValueB()); // why does it print 1?
}
}
Yes, but the return value is determined before the second call.
The value returned is determined by the evaluation of the expression in the return statement at that point in time - not "just before execution leaves the method".
From section 14.17 of the JLS:
Execution is then transferred to the
finally
block, as per section 14.20.2 of the JLS. That doesn't re-evaluate the expression in the return statement though.If your finally block were:
then that new return value would be the ultimate result of the method (as per section 14.20.2) - but you're not doing that.
The purpose of the finally method is to ensure that ressources are closed in any case. Consider the example:
The conn.close() statement is executed AFTER selectPersons(conn) is executed. Otherwise selectPersons(conn) would raise an connection closed error.
Because in you're getValue2() method, you're finally block just calls increment(), it does not return it. So what your code is doing is it increments and returns counter (1), and then increments counter to 2, but doesn't return it.
the
finally
block inGetValue2
method does not return anything. It's only calling the method to incrementcounter
.You do not have an explicit return in the second example. In this case, it will return the value within the
try
block. It makes intuitive sense becauseJava
has already executed the code inside thetry
block. It would not the executed that block again after executing thefinally
block.See my comment.
It would return
2
if you hadfinally { return increment(); }
. The firstreturn
statement's expression is evaluated prior to the finally block. See Section §14.20.2 of the JLS.Calling
getValue2
(as you have it now) twice would result in1
followed by3
.