note 95415 added to class.errorexception
| From: | triplepoint at gmail dot com | Date: | Sat, 02 Jan 2010 04:16:35 +0000 |
| Subject: | note 95415 added to class.errorexception | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164532@lists.php.net to get a copy of this message | ||
As noted below, it's important to realize that unless caught, any Exception thrown will halt
the script. So converting EVERY notice, warning, or error to an ErrorException will halt your
script when something harmlesss like E_USER_NOTICE is triggered.
It seems to me the best use of the ErrorException class is something like this:
<?php
function custom_error_handler($number, $string, $file, $line, $context)
{
// Determine if this error is one of the enabled ones in php config (php.ini, .htaccess, etc)
$error_is_enabled = (bool)($number & ini_get('error_reporting') );
// -- FATAL ERROR
// throw an Error Exception, to be handled by whatever Exception handling logic is available in
this context
if( in_array($number, array(E_USER_ERROR, E_RECOVERABLE_ERROR)) && $error_is_enabled ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
// -- NON-FATAL ERROR/WARNING/NOTICE
// Log the error if it's enabled, otherwise just ignore it
else if( $error_is_enabled ) {
error_log( $string, 0 );
return false; // Make sure this ends up in $php_errormsg, if appropriate
}
}
?>
Setting this function as the error handler will result in ErrorExceptions only being thrown for
E_USER_ERROR and E_RECOVERABLE_ERROR, while other enabled error types will simply get
error_log()'ed.
It's worth noting again that no matter what you do, "E_ERROR, E_PARSE, E_CORE_ERROR,
E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT" will never reach your
custom error handler, and therefore will not be converted into ErrorExceptions. Plan accordingly.
----
Server IP: 192.41.40.227
Probable Submitter: 71.81.35.228
----
Manual Page -- http://www.php.net/manual/en/class.errorexception.php
Edit -- https://master.php.net/note/edit/95415
Del: integrated -- https://master.php.net/note/delete/95415/integrated
Del: useless -- https://master.php.net/note/delete/95415/useless
Del: bad code -- https://master.php.net/note/delete/95415/bad+code
Del: spam -- https://master.php.net/note/delete/95415/spam
Del: non-english -- https://master.php.net/note/delete/95415/non-english
Del: in docs -- https://master.php.net/note/delete/95415/in+docs
Del: other reasons-- https://master.php.net/note/delete/95415
Reject -- https://master.php.net/note/reject/95415
Search -- https://master.php.net/manage/user-notes.php