Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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 didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic