PHP error_get_last,排除已捕获的异常

时间:2016-04-16 14:53:29

标签: php exception-handling uncaught-exception

我正在运行一些代码,例如:

register_shutdown_function("ShutdownHandler");

$var = test();

function test() {
    try {
        // Do something
        require_once("class.extra.php");
        ExtraClass::doSomething();
    } catch (Exception $e) {
        // Handle the error.
        echo "exception";
        return null;
    }
    // Continue...
    return true;
}

class.extra.php:

class ExtraClass
{
    public static function doSomething()
    {
        throw new CustomException("Exception");
    }
}

class CustomException extends Exception
{
    public function __construct($mesg, $code = 0x00)
    {
        parent::__construct($mesg, $code);
    }
}

在一些代码之后,脚本结束并且ShutdownHandler()被调用。

function ShutdownHandler() {
    // error_get_last returns the already catched exception and the next if get's triggered.
    $error = error_get_last();

    if ($error['type'] && E_ALL) {
        // Handle the uncaught exception...
        echo "uncaught exception";
    }
}

但我想在这里只处理未捕获的异常,任何想法?

0 个答案:

没有答案