Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return in try ,catch

 
sri vidya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • 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.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thankyou Ajith and Bill bozeman for your explanation.
Vidya.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic