php5 -> exceptions. used always?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

php5 -> exceptions. used always?

Post by jmut »

So are you using exceptions always when there is an error...no matter if critical or not.
Or you are using exceptions only for error that should stop further execution of the script.
Would like to hear your opinion about this.

I am just introducting myself to this concept and am interesting in different approaches.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

I'd say in PHP it is a 50-50, depending on what you are doing, if you are out for building that really robust generalized system with many many classes all over, then I'd say go exceptions, if you are considering small functions or only a small site, then using the old-style "return FALSE" etc is a really good way to not having to deal with all exceptions. But, in PHP I commonly mix quite freely, my usual strategy is to trigger critical errors by exceptions as they really should be handled or stop execution, whereas minor failures or likely non-occuring failures are reported with "return FALSE" etc. (It gets quite tedious having to create suitable classes for each exception and having to take care of each)

Understand as you see fit, I'd say that there is no rule for one or the other, just preference and situation.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I prefer to use trigger_error() to give a descriptive error. For example if the supplied argument is of the wrong type, then having a message to tell the developer (me in nearly all cases) that is infinitely more helpful than just returning false.

There is a point where it just does become excessive, such as in a once off 5min script, but for the most part well managed exceptions/errors are a 'must' for me.

:)
Post Reply