note 107264 added to function.set-error-handler

From: Date: Wed, 25 Jan 2012 21:08:34 +0000
Subject: note 107264 added to function.set-error-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-185745@lists.php.net to get a copy of this message
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! ---- Server IP: 69.147.83.197 Probable Submitter: 213.241.7.70 ---- Manual Page -- http://www.php.net/manual/en/function.set-error-handler.php Edit -- https://master.php.net/note/edit/107264 Del: integrated -- https://master.php.net/note/delete/107264/integrated Del: useless -- https://master.php.net/note/delete/107264/useless Del: bad code -- https://master.php.net/note/delete/107264/bad+code Del: spam -- https://master.php.net/note/delete/107264/spam Del: non-english -- https://master.php.net/note/delete/107264/non-english Del: in docs -- https://master.php.net/note/delete/107264/in+docs Del: other reasons-- https://master.php.net/note/delete/107264 Reject -- https://master.php.net/note/reject/107264 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185745) next »