• 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" statement inside try block

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm new here. Just read this strange code block, and tried it.
But I can't explain why the result is...



As we knew, the flow still come to finally block and assign r = 2 even we have return r; in try block.

But the result that test() return is 1, not 2.
If we comment out "return r;" at finally block, the result will be 2.
Confused !

Anyone can explain it ? :-)
[ May 20, 2005: Message edited by: Jonathan Nguyen ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This prints
try/return r : 1
finally r : 2
return value : 1

It appears that the return statement in the try block takes the value of r as it is in the try block and assigning r=2 in the finally has no effect as return line has already read r, If the return line is uncommented in the finally block the output is

try/return r : 1
finally r : 2
return value : 2

The r=2 line in finally block updates r and the return r; in the finally block is the return statement which occurs and the value is then 2.
Didnt know that would happen till i ran it there tho

//Tom
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jonathan,
As u know that a finally block always gets to execute either after a try block or a catch block. So only after the full completion of a try block or a catch block(If exception gets thrwon), the finally block executes.

So in ur program what i think is that return r is executed before the finally starts its execution. Return value is already set to be returned. So even though the return value gets changed in the finally block, the return is executed with the value set before in the try block.

Hope i am making some sense

Regards,
Animesh
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when the try block runs, you set the return value. the 'return r' line doesn't set r as what's returned, but the VALUE of r to be returned.

then you enter the finally block, and assign r to 2. we leave the try block, and now return that value we stored before, which is still 1.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic