Forums Register Login

Finally block execution

+Pie Number of slices to send: Send

Hello Friends,

I tried to execute the below method and i got the output "Inside try block" surprisingly. Could anyone explain why it is printing "Inside try block" instead of "Inside finally block"? Its like we are assigning new value to the strValue variable. However, still i am not able to understand why it is printing "Inside try block".



+Pie Number of slices to send: Send
The return statement doesn't return the variable, it returns the value of the variable and the value of the variable when you called return was 'Inside try block'. Changing the value of the variable in the finally block doesn't affect the value that is returned.
+Pie Number of slices to send: Send
Agreed. However, the string variable's value always replaces to new one when we assign new value to it if i am not wrong. The value or the strValue must be pointing to some memory address which contains the updated variable value. Right?
+Pie Number of slices to send: Send
 

Rahul Nair wrote:Agreed. However, the string variable's value always replaces to new one when we assign new value to it if i am not wrong. The value or the strValue must be pointing to some memory address which contains the updated variable value. Right?


Yes. But as I said, the value of the variable at the time the return statement is called is what is returned. The return statement puts this value on the stack ready to be returned, before the finally block is called. At this point there is no connection between the variable and the value that is going to be returned.
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 763 times.
Similar Threads
Exceptions
Return statement in try block
Doubt in try finally .......
exception
Try an Finally Working Confusion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:57:25.