문제

I've written the following custom Exception handler:

namespace System\Exception;

class Handler extends \Exception {


    public static function getException($e = null) {

        if (ENVIRONMENT === 0 && is_object($e)) {       
            $message  = "<p>";
            $message .= "Exception: " . $e->getMessage();
            $message .= "<br />File: " . $e->getFile();
            $message .= "<br />Line: " . $e->getLine();
            $message .= "<br />Trace: " . $e->getTrace();
            $message .= "<br />Trace as string: " . $e->getTraceAsString();
            $message .= "</p>";
        } else {
            $message  = '<h1>Exception</h1>';
            $message .= '<p>There was a problem.</p>';
        }

        @require_once('header.php');
        echo $message;
        @require_once('footer.php');

        exit();

    }


    public static function getError($errno = 0, $errstr = null, $errfile = null, $errline = 0) {
        if (ENVIRONMENT === 0) {        
            $message  = "<p>";
            $message .= "Error: " . $errstr;
            $message .= "<br />File: " . $errfile;
            $message .= "<br />Line: " . $errline;
            $message .= "<br />Number: " . $errno;
            $message .= "</p>";
        } else {
            $message  = '<h1>Error</h1>';
            $message .= '<p>There was a problem.</p>';
        }

        @require_once('header.php');
        echo $message;
        @require_once('footer.php');

        exit();

    }   


    public static function getShutdown() {
        $last_error = error_get_last();
        if ($last_error['type'] === E_ERROR) {
            self::getError(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
        }
    }


}

and have indicated that I want to use this class and its methods for processing all exceptions and errors generated by the system in the following way:

set_exception_handler(array("System\Exception\Handler", "getException"));
set_error_handler(array("System\Exception\Handler", "getError"), -1 & ~E_NOTICE & ~E_USER_NOTICE);
register_shutdown_function(array("System\Exception\Handler", "getShutdown"));

I have also indicated that I don't want errors to be displayed on the screen and wand to report all of the errors:

ini_set('display_errors', 'Off');
error_reporting(-1);

My question now is - do I still need to use the try { } catch () { } statement in order to catch any exceptions and errors? I know that the above is most probably not a bullet proof, but seem to be working so far without any try / catch statement by processing all uncaught exceptions and errors.

Also - is there any disadvantage by using custom exception handler and letting it catch all uncaught exceptions rather than doing this via try {} catch (i.e. performance / security etc.)?

도움이 되었습니까?

해결책

You don't have to, but you cannot recover - using try/catch gives you the advantage of reacting to particular exception (for example file not found in some_custom_session_handling() might be a good place to use try/catch and log out such user without session file).

So the advantage is that you have prettier messages. The downside is that you treat exceptions always the same. It's not bad in itself, and should not degradate performance or security, but it misses the point of using exceptions in the first place.

However, it does not exclude using try/catch where you might want them, so I'd say it's a good failover solution, but should be avoided as a try/catch replacement

다른 팁

As jderda said, be using your aproach you are missing the point of exceptions: to check for any errors in the upper levels of your code and react to them - halt or treat the exception and move on. Your aproach is fine when you want to, for example, log all uncaught exceptions

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top