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:

  1. a custom exception handler let display nice messages, errors, using set_exception_handler.
  2. it not disrupt existing code in way... trigger_error , other error functions still work normally.
  3. it makes hard ignore stupid coding mistakes trigger e_notices , e_warnings.
  4. 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; } 
  5. 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

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -