Exceptions, ur doin it wrong
By Adam Kinder on Oct 14, 2008 in Programming
Ok, I’ve seen this crap come up like ten times in the past month. Usually from PHP “developers” unfortunately, even those with the ZCE badge of honor ( I expect better from you guys! ).
Exceptions are NOT for userland errors. Throwing an exception, especially in PHP, is very expensive.
This is another perfect example of PHP adopting a methodology from another language, and then not guiding their developers in the right direction.
This, is wrong:
if( !$user_email )
{
throw new Exception( "You have to fill out the email field" );
}
A good rule of thumb, if it couldn’t fit logically in a try/catch statement, then it’s not an exception. This is a good use for exceptions:
if( !$dbInstance )
{
// Something went wrong with the DB connection
try
{
$dbInstance = new dbObject();
}
catch( MyDBException $e )
{
print "Something broked: ".$e->getMessage();
}
}

All about the Kinder™
How do people consider not filling out the complete form an exception? Exceptions should only be used for semi-unpredictable errors caused by the application. I can very well predict someone is not going to fill in every field once in a while. Keyword was “someone”, and not the “application”.
ur website needz syntacks highlighter.
srsly, I can’t stand when it strips out the friggin slashes
http://wordpress.org/extend/plugins/wp-syntax/ There’s a good syntax highlighter for ya. Sexy, easy, yay.
Honestly, I’ve never found a need for exceptions. Does this make me fail?
Nein, I don’t use them in PHP. I use them a lot in Java though
Aye, Java run-time exceptions FTW.
I have yet to find any instance of them in our PB applications either.