/    Sign up×
Community /Pin to ProfileBookmark

Overhead using try/catch?

I’m trying to develop a skeleton of common classes to use in my PHP apps, which will involve multiple layers of classes and includes. I’m currently trying to determine the best method for error handling. My first thought was to universally handle errors by throwing exceptions, and putting the very top layer of the code (i.e., the index page) inside a try/catch block.

Is this resource efficient, though? Does running code in a try/catch increase overhead, and is it a bad idea to essentially have all site code in each request running in a try/catch block?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 26.2009 — I'm with the camp that feels that exceptions should be for exceptional events outside the expected, "normal" flow of the program. As extreme examples, the user entering a 4-character US Zip Code would be an error, and the normal program flow should include error-handling to deal with it; while a database query that returns an SQL syntax error would be an exceptional (hopefully!) event that should be handled by throwing an exception.

This will still leave a lot of room for personal preference as to where the dividing line is between the two, but, personally, I would not go so far as to handle every error via exceptions.
Copy linkTweet thisAlerts:
@tarsusauthorAug 26.2009 — You're right, and I realize I need to clarify: Certainly, errors that aren't "fatal" will be handled differently. But as a technique for handling any of those errors that [I]are[/I] fatal, is it detrimental to my resources for all code to be running in a single try/catch? Should I be using multiple try/catch blocks in the smaller sections of code where they are relevant?
Copy linkTweet thisAlerts:
@NogDogAug 26.2009 — I'm not aware of any particular performance issues. I suppose there's [i]some[/i] extra processing, but presumably it's just setting a value somewhere in the stack that indicates where the processing is to go should an exception be thrown; a few microseconds to parse/compile and a few more to implement at run-time: nothing I'd lose any sleep over.

Also for your consideration would be using [url=http://php.net/set_exception_handler]set_exception_handler[/url]() at the top level of your application for the handling of any exceptions not caught by internal try/catch blocks. Not that this would be any better processing-wise, it just might make for cleaner code, moving the details of the "catch" at the top level into a separate class or function.
Copy linkTweet thisAlerts:
@tarsusauthorAug 27.2009 — Ah, I was not aware of that function. Thanks a lot for all your help!
Copy linkTweet thisAlerts:
@MindzaiAug 27.2009 — I would agree with Nogdog - I can't see exceptions being anywhere near resource-intensive enough to worry about. Aside from a tiny bit of internal processing and stack examination, and the memory required to store an instance of the appropriate exception class (which tend to be small and simple) then I can't think of what more there is to do. Granted it's probably going to be slower than handling errors inline, but we're probably talking thousandths of a second or less, and for the benefits it gives you as a programmer I would say it's resource well spent.
×

Success!

Help @tarsus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...