• 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

finally and return

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi:
Consider the following situation:

According to JLS finally clause always run, even after return.
consider the following scenarios:
1- try block excuted successfully, then when finally clause will run?
before first return, of after first return? If after first return, then second and third return will never be excuted.
2- try block isn't excuted successfully, then which will run first, catch or finally. will second return ever be excuted??
In any given scenarios, will the third return ever get to be excuted?
I appreciated if someone can explain what happen inside the JVM to excute the finally clause. If a try has a return, the the method exit and the stack will be terminated, or the retun will be put in hold until the finally clause excuted then the return statement will excute. in this case what will happen if I have 2 returns, on in finnaly and the other in try. Which one will get to run??
Edited by Corey McGlone: Added CODE Tags
[ January 20, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I recommend. Put this code into a shell of a program and try a few different things to see what happens. I've taken the first step of constructing the shell for you:

Now, you can run this as is and see how the flow works. Of course, you can change little things like commenting out "Line E" in the throwException() method to see how the flow would work if no exception was thrown in the try block.
I'll give you one tip right off the bat, though - the final return statement (following the finally clause) will cause a compiler error every time. Do you know why?
Play with that a bit and see what you think. If you're confused about the flow, feel free to ask, but I think you'll gain a better understanding if you play with it a bit.
Corey
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Corey. I should have coded a test case before I post my quistion, but I think I got lazy. I coded it now, and I believe I got the handle of it.
Thanks again
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic