5

I came across this question and I am unable to understand the reason of the output it is giving.

The program is:

public static String method(){

    String s = new String();
    try
    {
        s = "return value from try block";
        return s;
    }
    catch (Exception e)
    {
        s = s + "return value from catch block";
        return s;
    }
    finally
    {
        s = s + "return value from finally block";
    }

}

The output is:

return value from try block

Now, I debugged it and the value of s at return s statement in try block was return value from try block, return value from catch block after it returned from finally block.

Still the output is :

return value from try block

Can any one please explain this behavior?

Vikas
  • 107
  • 1
  • 10
  • See this one https://stackoverflow.com/questions/65035/does-finally-always-execute-in-java – mmuzahid Jun 04 '17 at 06:26
  • 1
    I think you'll find more clarification here [Try-catch-finally-return clarification - Stack Overflow](https://stackoverflow.com/questions/15225819/try-catch-finally-return-clarification) – Alan Lal Jun 04 '17 at 06:29
  • The answer would be from oracle documentation: "The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs." check this refrence : https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html – Oghli Jun 04 '17 at 06:48

3 Answers3

1

First of all, let us understand how Java handles method calls. There is separate stack memory for every thread. When we call any method, Java internally inserts a record at top of the stack. The record contains some details like parameters & object etc.

Here one interesting field is there: the return value. Whenever​ the return statement is encountered, the field is updated. Now observe the code.

In the try block, the return value field is set to the return value from the try block. Now as per the execution sequence the finally block is executed. Now the finally block modifies the string reference s. It didn't modify the return value field which is previously settled by try block. Finally​ the method call completed & Java internally pop the record & return the return value field. So it returns the return value from the try block.

If the finally block return the string after modification, then the return value field is updated again and the updated string will be returned.

Neel Patel
  • 358
  • 2
  • 13
-1

Avoid return statement in try block if you include finally in your code instead use it at the end of the method. Also, you can't modify the value of the returned variable in finally block. See https://stackoverflow.com/a/3643047/6286156

-2

finally

finally is used in a try/catch statement to execute code "always"

lock.lock();
try {
  //do stuff
} catch (SomeException se) {
  //handle se
} finally {
  lock.unlock(); //always executed, even if Exception or Error or se
}

Java 7 has a new try with resources statement that you can use to automatically close resources that explicitly or implicitly implement java.io.Closeable or java.lang.AutoCloseable

amran_bd
  • 152
  • 2
  • 12