note 120196 added to function.set-exception-handler

From: Date: Mon, 21 Nov 2016 15:51:21 +0000
Subject: note 120196 added to function.set-exception-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-207258@lists.php.net to get a copy of this message
For those of you wanting to convert PHP errors to ErrorExceptions (useful), but frustrated with the script being halted on every E_NOTICE et al. In your error handler, simply create the ErrorException, and then either throw it (script halted), or pass the object directly (script continues) to your exception handler function. <?php const EXIT_ON_ALL_PHP_ERRORS = false; // or true function proc_error($errno, $errstr, $errfile, $errline) { $e = new ErrorException($errstr, 0, $errno, $errfile, $errline); if (EXIT_ON_ALL_PHP_ERRORS) { throw $e; // This will halt your script. } else { proc_exception($e); // This will let it continue. } } set_error_handler("proc_error"); set_exception_handler("proc_exception"); ?> You could further customize the error severity level (from $errno, match bitmasks with error level constants) at which the script is halted or allowed to continue. The above simply allows passthru for PHP errors' default behavior. If your exception handler is receiving both error ErrorExceptions (with severity level etc.) and other types of uncaught Exceptions, then use a <?php if ($e instanceof ErrorException) { // ... } ?> condition to deal with the variance. I had to further wrap it into a <?php try { $errno = $e->getSeverity(); } catch (Exception $x) { $errno = 0 } ... ?>, because some EE-s were mysteriously lacking the severity level property (yet to dig in and find out why). ---- Server IP: 202.182.182.181 Probable Submitter: 114.121.238.46 ---- Manual Page -- http://php.net/manual/en/function.set-exception-handler.php Edit -- https://master.php.net/note/edit/120196 Del: integrated -- https://master.php.net/note/delete/120196/integrated Del: useless -- https://master.php.net/note/delete/120196/useless Del: bad code -- https://master.php.net/note/delete/120196/bad+code Del: spam -- https://master.php.net/note/delete/120196/spam Del: non-english -- https://master.php.net/note/delete/120196/non-english Del: in docs -- https://master.php.net/note/delete/120196/in+docs Del: other reasons-- https://master.php.net/note/delete/120196 Reject -- https://master.php.net/note/reject/120196 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#207258) next »