Voting

: two minus one?
(Example: nine)

The Note You're Voting On

marques at displague dot com
17 years ago
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.

<< Back to user notes page

To Top