note 88082 added to function.set-exception-handler
| From: | joshua dot boyle-petrie at its dot monash dot edu | Date: | Fri, 09 Jan 2009 07:44:19 +0000 |
| Subject: | note 88082 added to function.set-exception-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148980@lists.php.net to get a copy of this message | ||
Thanks to mastabog we know that throwing an exception within the exception handler will trigger a
fatal error and a debugging nightmare. To avoid throwing an exception within there should be easy.
However, if you use a custom error handler to convert errors to ErrorExceptions suddenly there are a
multitude of new ways to accidentally throw exceptions within the exception handler.
<?php
function error_handler($code, $message, $file, $line)
{
if (0 == error_reporting())
{
return;
}
throw new ErrorException($message, 0, $code, $file, $line);
}
function exception_handler($e)
{
// ... normal exception stuff goes here
print $undefined; // This is the underlying problem
}
set_error_handler("error_handler");
set_exception_handler("exception_handler");
throw new Exception("Just invoking the exception handler");
?>
Output: Fatal error: Exception thrown without a stack frame in Unknown on line 0
The best way I have found to avoid this is to wrap up everything in the exception handler in a
try/catch block.
<?php
function exception_handler($e)
{
try
{
// ... normal exception stuff goes here
print $undefined; // This is the underlying problem
}
catch (Exception $e)
{
print get_class($e)." thrown within the exception handler. Message:
".$e->getMessage()." on line ".$e->getLine();
}
}
?>
Output: ErrorException thrown within the exception handler. Message: Undefined variable: undefined
on line 14
This speeds up debugging and offers some scalability to any other exceptions accidentally thrown
within the exception handler.
Another solution is to restore the error handler at the beginning of the exception handler. While
this is a silver bullet in terms of avoiding the ErrorExceptions, debugging messages then rely on
the error_reporting() level and the display_errors directive. Why mention this? It might be
preferable for production code since we care more about hiding errors from users than convenient
debugging messages.
----
Server IP: 117.55.229.1
Probable Submitter: 130.194.81.30
----
Manual Page -- http://www.php.net/manual/en/function.set-exception-handler.php
Edit -- https://master.php.net/note/edit/88082
Del: integrated -- https://master.php.net/note/delete/88082/integrated
Del: useless -- https://master.php.net/note/delete/88082/useless
Del: bad code -- https://master.php.net/note/delete/88082/bad+code
Del: spam -- https://master.php.net/note/delete/88082/spam
Del: non-english -- https://master.php.net/note/delete/88082/non-english
Del: in docs -- https://master.php.net/note/delete/88082/in+docs
Del: other reasons-- https://master.php.net/note/delete/88082
Reject -- https://master.php.net/note/reject/88082
Search -- https://master.php.net/manage/user-notes.php