aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return in try ,catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return in try ,catch" Watch "return in try ,catch" New topic
Author

return in try ,catch

sri vidya
Greenhorn

Joined: Oct 10, 2000
Posts: 5
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Mar 17, 2000
Posts: 5782
Checkout one of my very old post on this topic
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Thanks Ajith, that explains it better than I was able to do.
sri vidya
Greenhorn

Joined: Oct 10, 2000
Posts: 5
Thankyou Ajith and Bill bozeman for your explanation.
Vidya.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return in try ,catch