This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Efficient Try Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Efficient Try Catch" Watch "Efficient Try Catch" New topic

Efficient Try Catch

Joe Deluca

Joined: Jan 29, 2009
Posts: 14
Hey everybody!

I was just wondering which is more efficient, a try/catch in the method, or try/catch the actual method. Such as:


What are your thoughts?
Thanks, Joe

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38057
It all depends what the method is doing, what sort of Exception it throws, and what you want to do with it. Are you inside a loop which can be repeated when whatever goes wrong has been sorted out? Do you have to close your app if there is an Exception?

But simply writing "return;" in a catch block is very inefficient. The most you will achieve like that is hiding errors; an Exception you know about is not dangerous. An Exception you don't know about, because it is consumed silently, can be very dangerous because you might be calculating "wrong" answers and not know about it. At least print a message so you know what has happened.

And you thought I would say "no 1" or "no 2"!
I agree. Here's the link:
subject: Efficient Try Catch
Similar Threads
How to check if String() value is numeric
Return Statement from a Method
Casting Throwbale to Exception
how to refactor this method ?