• 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 in try ,catch

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please tell me the purpose of return in catch or finally block. what will happen if we don't place return.

thanks in advace
Vidya
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to put a return statement in a catch or finally block, and I would only do so if I wanted the rest of the method to stop after the finally statement. Generally it will work like this:
With no return statement:
If an error arises in try block, execution moves to coorsponding catch block, then to the finally block, then to rest of the method.
With a return statement in the catch or finally block:
If an error arises in try block, execution moves to coorsponding catch blcok, then to the finally block, then out of the method since you returned a value.
So I would use a return statement if for one exception you wanted the rest of the method to continue after the finally block, but say for another exception you wanted the method to stop executing. The reason I say it this way is because if you wanted the method to not doing anything else after the finally block, you could just wrap the whole method in the try block, but that doesn't work if you want different exceptions to behave differently. Does this make sense? I'll include code if this is confusing.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checkout one of my very old post on this topic
Ajith
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith, that explains it better than I was able to do.
 
sri vidya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Ajith and Bill bozeman for your explanation.
Vidya.
 
reply
    Bookmark Topic Watch Topic
  • New Topic