In regards to Error handling in PHP -- As far I know there are 3 styles:
die()
orexit()
style:$con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }
throw Exception
style:if (开发者_StackOverflow社区!function_exists('curl_init')) { throw new Exception('need the CURL PHP extension. Recomplie PHP with curl'); }
trigger_error()
style:if(!is_array($config) && isset($config)) { trigger_error('Error: config is not an array or is not set', E_USER_ERROR); }
Now, in the PHP manual all three methods are used.
What I want to know is which style should I prefer & why?
Are these 3 drop in replacements of each other & therefore can be used interchangeably?
Slightly OT: Is it just me or everyone thinks PHP error handling options are just too many to the extent it confuses php developers?
The first one should never be used in production code, since it's transporting information irrelevant to end-users (a user can't do anything about "Cannot connect to database").
You throw Exceptions if you know that at a certain critical code point, your application can fail and you want your code to recover across multiple call-levels.
trigger_error()
lets you fine-grain error reporting (by using different levels of error messages) and you can hide those errors from end-users (using set_error_handler()
) but still have them be displayed to you during testing.
Also trigger_error()
can produce non-fatal messages important during development that can be suppressed in production code using a custom error handler. You can produce fatal errors, too (E_USER_ERROR
) but those aren't recoverable. If you trigger one of those, program execution stops at that point. This is why, for fatal errors, Exceptions should be used. This way, you'll have more control over your program's flow:
// Example (pseudo-code for db queries):
$db->query('START TRANSACTION');
try {
while ($row = gather_data()) {
$db->query('INSERT INTO `table` (`foo`,`bar`) VALUES(?,?)', ...);
}
$db->query('COMMIT');
} catch(Exception $e) {
$db->query('ROLLBACK');
}
Here, if gather_data()
just plain croaked (using E_USER_ERROR
or die()
) there's a chance, previous INSERT
statements would have made it into your database, even if not desired and you'd have no control over what's to happen next.
I usually use the first way for simple debugging in development code. It is not recommended for production. The best way is to throw an exception, which you can catch in other parts of the program and do some error handling on.
The three styles are not drop-in replacements for each other. The first one is not an error at all, but just a way to stop the script and output some debugging info for you to manually parse. The second one is not an error per se, but will be converted into an error if you don't catch it. The last one is triggering a real error in the PHP engine which will be handled according to the configuration of your PHP environment (in some cases shown to the user, in other cases just logged to a file or not saved at all).
精彩评论