note 82512 added to function.set-exception-handler

From: Date: Mon, 14 Apr 2008 14:19:54 +0000
Subject: note 82512 added to function.set-exception-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-138297@lists.php.net to get a copy of this message
frank, Your exception handler is configured to be the handler for all exceptions, yet if a basic 'Exception' is thrown, your static method will error because 'Exception's do not have 'getException'. Because of this I don't see a real purpose to making the uncaught handler a class that extends Exception. I do like the idea of using static methods of a general Exception handling class. <?php class ExceptionHandler { public static function printException(Exception $e) { print 'Uncaught '.get_class($e).', code: ' . $e->getCode() . "<br />Message: " . htmlentities($e->getMessage())."\n"; } public static function handleException(Exception $e) { self::printException($e); } } set_exception_handler(array("ExceptionHandler", "handleException")); class NewException extends Exception {} try { throw new NewException("Catch me once", 1); } catch (Exception $e) { ExceptionHandler::handleException($e); } throw new Exception("Catch me twice", 2); ?> Gives: Uncaught NewException, code: 1<br />Message: Catch me once Uncaught Exception, code: 2<br />Message: Catch me twice There are much more interesting things that can be done like reformating and optionally displaying or emailing them. But this class acts a nice container for those functions. ---- Server IP: 209.41.74.194 Probable Submitter: 208.236.9.10 ---- Manual Page -- http://www.php.net/manual/en/function.set-exception-handler.php Edit -- https://master.php.net/note/edit/82512 Del: integrated -- https://master.php.net/note/delete/82512/integrated Del: useless -- https://master.php.net/note/delete/82512/useless Del: bad code -- https://master.php.net/note/delete/82512/bad+code Del: spam -- https://master.php.net/note/delete/82512/spam Del: non-english -- https://master.php.net/note/delete/82512/non-english Del: in docs -- https://master.php.net/note/delete/82512/in+docs Del: other reasons-- https://master.php.net/note/delete/82512 Reject -- https://master.php.net/note/reject/82512 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138297) next »