php - In PHP5, should I use Exceptions or trigger_error/set_error_handler? -
what pros/cons of doing either way. there 1 right way(tm) ?
if want use exceptions instead of errors entire application, can errorexception , custom error handler (see errorexception page sample error handler). downside method non-fatal errors still throw exceptions, fatal unless caught. basically, e_notice
halt entire application if error_reporting settings not suppress them.
in opinion, there several benefits using errorexception:
- a custom exception handler let display nice messages, errors, using set_exception_handler.
- it not disrupt existing code in way... trigger_error , other error functions still work normally.
- it makes hard ignore stupid coding mistakes trigger
e_notice
s ,e_warning
s. you can use
try
/catch
wrap code may generate php error (not exceptions), nice way avoid using@
error suppression hack:try { $foo = $_get['foo']; } catch (errorexception $e) { $foo = null; }
you can wrap entire script in single
try
/catch
block if want display friendly message users when uncaught error happens. (do carefully, because uncaught errors , exceptions logged.)
Comments
Post a Comment