note 107264 deleted from function.set-error-handler by joey
| From: | joey@php.net | Date: | Sat, 06 Apr 2013 22:33:50 +0000 |
| Subject: | note 107264 deleted from function.set-error-handler by joey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-194364@lists.php.net to get a copy of this message | ||
Note Submitter: bartek at webph-no-dash-es-oto dot pl
----
I've found, that instead of throwing exceptions in error_handler just to immediately catch them
in exception_handler is very unpractical.
Instead, I simply transfer them directly as function arguments, like this:
<?php
set_error_handler('my_error_handler');
set_exception_handler('my_exception_handler');
function my_exception_handler($e) {
exit('Huston! We have a problem: '.$e);
}
function my_error_handler($no,$str,$file,$line) {
$e = new ErrorException($str,$no,0,$file,$line);
my_exception_handler($e); /* Do not throw, simply call error handler with exception object */
}
?>
The effect is exactly the same (application stopped), plus you cannot catch such exceptions
(ErrorException) as those never get thrown.
Catching theese errors (ErrorException) can produce rather unexpacted results like errors at line 0
for instance.
Your application might be running, when under normal conditions (error reporting) it would not.
Also, I would consider catching Exception class as bad practice (ErrorException inherits from it
too). You should always
create your lowest defined error and never catch anything lower then that:
<?php
class MyException extends Exception {}
//...
try {
throw new MyException('Something is burning ...');
} catch (MyException $e) {
// It's our exception, it is most likely friendly :)
} catch (Exception $e) {
//Really bad practice, as you CANNOT know who and why thrown that.
}
?>
Just my 3 cents, peace!