note 54482 added to function.set-error-handler
| From: | dawiddr at gmail dot com | Date: | Tue, 05 Jul 2005 23:01:53 +0000 |
| Subject: | note 54482 added to function.set-error-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91492@lists.php.net to get a copy of this message | ||
In PHP5, if you want to have exceptions thrown instead of normal errors - you could use an error
handler, which throw exceptions:
<?php
function handler($errno, $errstr, $errfile, $errline)
{
print "Error handled!\n";
throw new Exception($errstr, $errno);
}
set_error_handler('handler');
try
{
print 2 / 0; // simple error - division by zero
print "This will never be printed";
}
catch (Exception $e)
{
print "Exception catched:\n";
print "Code: ".$e->getCode()."\n";
print "Message: ".$e->getMessage()."\n";
print "Line: ".$e->getLine();
}
?>
Result:
Error handled!
Exception catched:
Code: 2
Message: Division by zero
Line: 6
As you see, exception is catched like it have been thrown by division by zero - in try/catch clause.
But line numer and backtrace shows, that is was thrown by the error handler.
----
Manual Page -- http://www.php.net/manual/en/function.set-error-handler.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54482
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54482&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54482&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54482&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54482&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54482&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54482&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54482&report=yes
Search -- http://master.php.net/manage/user-notes.php