• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return value from try-catch-finally block.

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the sample code written below :

public class ReturnExample
{
private String normalExecution()
{
String result = "";
try
{
result = "Entered try block.";
return result;
}
catch(Exception e)
{
result = result + "Entered Catch block.";
return result;
}
finally
{
result = result + "Entered finally block.";
}
}

public static void main(String[] args)
{
ReturnExample example = new ReturnExample();

String result = example.normalExecution();
System.out.println(result);

}
}

Why is the result of the program being printed as �Entered try block�.
Although finally block gets executed but the returned value from the function call only contains the string value set in the try block and doesn�t includes the changes done in finally block.

Thanks!!
 
Ranch Hand
Posts: 368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are getting the o/p "Entered try block" as this is what you are returning from the method. Though the finally block is executed, the return value has already been decided in the try block. Hence appending result = result + "Entered finally block."; has no impact on the return value. To verify this, try putting the return in finally block too:



I think then you would get the expected output. Please correct me if am missing something here.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per the try block you are returning "result" to main method.

Only you are returning this result value to string "result" in main method.
After assignment of the return statement finally block will be executed.

So in the finally block no changes coming for the result string. bz it is returning try block result.

Regards,
Shankar
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true. Adding return statement in finally block returns the string with the value set in finally block to it.

-----------------------------------------------------------------------------
Though the finally block is executed, the return value has already been decided in the try block. Hence appending result = result + "Entered finally block."; has no impact on the return value."
-----------------------------------------------------------------------------

But i am not convinced with the statement that "the return value has already been decided in the try block". I am setting the String reference to point to a new value. Then why is it returning the old value.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gaurav Bhatia:
I am setting the String reference to point to a new value. Then why is it returning the old value.

Since the compiler makes a copy of the reference variable before the finally block is executed.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gaurav Bhatia:
Please check the sample code written below :

public class ReturnExample
{
private String normalExecution()
{
String result = "";
try
{
result = "Entered try block.";
return result;
}
catch(Exception e)
{
result = result + "Entered Catch block.";
return result;
}
finally
{
result = result + "Entered finally block.";
}
}

public static void main(String[] args)
{
ReturnExample example = new ReturnExample();

String result = example.normalExecution();
System.out.println(result);

}
}

Why is the result of the program being printed as �Entered try block�.
Although finally block gets executed but the returned value from the function call only contains the string value set in the try block and doesn�t includes the changes done in finally block.

Thanks!!



let's analyze this code of yours....

you created a String type reference variable named "result".... then you are assigning the value returned by the method "normalExecution()" to this reference variable "result".

now lets see..the method starts executing and we.... go into the try block......
now there's a Private Variable(local to the method)declared in the method...
this gets the value...."Entered try Block"....

now as soon as this step is reached... in the next step we are returning the
value stored in the result(local variable ) to the main method... and then the we enter the finally block...
the value of the String result(local variable) is again updated....
now... as per our calculations.... the result should be...

"Entering try Block"
"Entering finally Block.."

but we have got
"Entering try block" as the answer....

why....??


reason:

because we have returned the value of the local variable, result, as soon as it is modified to "Entering try Block", to the main... and this value will be assigned to the variable "result" in the main method...
in the next step...we asked the compiler to print the result variable(main method)...so we get the result as "Entering the try Block"


if we had placed the return statement in the try block, instead in the finally block.. we would have got the result...
"Entering try Block"
"Entering Finally Block."

understand....
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,
In this example, I use your original code but I change de type of result variavel to StringBuffer. In this case, the result is "Entered try block. Entered Catch block.".



Alexsandra
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandeep atluri:




if we had placed the return statement in the try block, instead in the finally block.. we would have got the result...
"Entering try Block"
"Entering Finally Block."

understand....



I think you meant putting return in finally rather than try block. What you said is no different from the code already posted .
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexsandra, it works as expected when using StringBuffer variable.(as you mentioned).

Also, by returning a user defined class's instance/object, the changes done in finally are reflected back in main method (even if return statement is placed in try block).(attached code shows the usage)

public class ReturnExample1
{
private Data normalExecution()
{
Data result = new Data();
try
{
result.setValue(10);
return result;
}
catch(Exception e)
{
result.setValue(20);
return result;
}
finally
{
result.setValue(30);

}
}

public static void main(String[] args)
{
ReturnExample1 example = new ReturnExample1 ();

Data result = example.normalExecution();
System.out.println(result.getValue());
//Value returned is 30.
}
}

class Data
{
int variable = 0;
public void setValue(int i)
{
variable = i;
}
public int getValue()
{
return variable;
}
}


I tried using an int variable, it also gives the same result as with String.

So, the behaviour is different in case of Strings and primitive types.
 
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me add my 2 cents

spec says return statement with an expression, first evaluates the expression and then tries to return the value of the expression. But if there exists any associated finally block, it executes that before returning. So when you are returning some immutable object or primitive, changed value in finally block will not be reflected in return value, since that is already computed. For the same reason if you return some (immutable/mutable) object o and assign some other object in o in finally block, then that will also not be reflected in return type.

I hope all this make sense. thanks.
[ August 20, 2007: Message edited by: Sahid Khan ]
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sahid!!
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurav Bhatia wrote:Alexsandra, it works as expected when using StringBuffer variable.(as you mentioned).

Also, by returning a user defined class's instance/object, the changes done in finally are reflected back in main method (even if return statement is placed in try block).(attached code shows the usage)

public class ReturnExample1
{
private Data normalExecution()
{
Data result = new Data();
try
{
result.setValue(10);
return result;
}
catch(Exception e)
{
result.setValue(20);
return result;
}
finally
{
result.setValue(30);

}
}

public static void main(String[] args)
{
ReturnExample1 example = new ReturnExample1 ();

Data result = example.normalExecution();
System.out.println(result.getValue());
//Value returned is 30.
}
}

class Data
{
int variable = 0;
public void setValue(int i)
{
variable = i;
}
public int getValue()
{
return variable;
}
}


I tried using an int variable, it also gives the same result as with String.

So, the behaviour is different in case of Strings and primitive types.




Dear Gaurav,

You might know that String and all wrapper classes are immutable and rest of all (StringBuffer and Other classes (in your case Data) are mutable) thats why any changes in instance reflects to the related instance like in finally block.
So the result differes in both of the cases(String and StringBuffer as well as Data class).
reply
    Bookmark Topic Watch Topic
  • New Topic