• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finally block execution  RSS feed

 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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".



 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!