Click here to Skip to main content
15,887,349 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need explanation for the below code.

Java
    public static void main(String[] args)
    {
        System.out.println(methodReturningValue());
    }

    static String methodReturningValue()
    {
        String s = null;
        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";
        }
    }
}
public class ReturnValueFromTryCatchFinally
{


What I have tried:

My understanding is that finally block will be executed always and so that the value of s will be "return value from try block return value from finally block". But the above code gives me the value of s from try block. i.e; return value from try block. Why is that the value of s is returning from try block . As far as I know Finally block will be always executed. So won't the value of s be changed? Is there anything that I need to understand on Scope of the variable inside try, catch block.
Posted
Updated 26-May-18 5:51am
v3
Comments
Richard MacCutchan 26-May-18 8:55am    
Because you are using return in the try block. And the finally clause executes after the end of the try block. Use your debugger to see the actual sequence of events.

1 solution

Strings are immutable. Once created, they cannot be changed. In your "s = s +" statements, you're allocating a new string with the result of the operation.

In the Try block, you allocated a string, "return value from try block". You then pushed the pointer to that string on the stack using the return statement.

Now, in your Finally block, you concatenate two strings together and then don't do anything with the resulting string. Remember, the pointer to the original string is what was pushed onto the stack for return to the caller, not a "value of s" or the pointer to the new string.

Now, if you try putting a return statement in the Finally block, you'll find that you can't.

The proper way to do this would be to NOT put the returns in the Try and Catch blocks, but after the try/catch/finally block entirely.
C#
public static void main(String[] args)
{
    System.out.println(methodReturningValue());
}

static String methodReturningValue()
{
    string s = null;

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

    return s;
}

Yes, I'm one of those people who subscribes to the "one return statement per method" paradigm.
 
Share this answer
 
v4
Comments
[no name] 26-May-18 15:25pm    
Wow, that one is great, a 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900