0

エラーと例外の中心的なエラー処理関数としてErrorExceptionを使用しています。

例:

set_error_handler('my_error_handler');
set_exception_handler('my_exception_handler');    

function my_error_handler ($errno, $errstr, $errfile, $errline)
{
    my_exception_handler(new ErrorException($errstr, 0, $errno, $errfile, $errline));
}

function my_exception_handler ($e)
{   
    echo $e->getSeverity();
    echo $e->getCode();
    echo $e->getMessage();          
}

現在は、デフォルト$e->getSeverity();にのみ存在しErrorException、デフォルトには存在しないように見えますException

throw new Exception('test');私が得るときFatal error: Call to undefined method Exception::getSeverity()

しかし、私throw new ErrorException('test');が期待どおりに機能するとき。

これは論理的に思えますがthrow new Exception('test');、致命的なエラーが発生せずに使用できる方法はありますか?ほとんどのサードパーティライブラリは(ErrorExceptionではなく)例外を使用するため、サードパーティの例外が発生すると問題が発生し、致命的なエラーが発生します。どうすればこれを解決できますか?

4

1 に答える 1

5

キャッチされた例外が次のものであるかどうかを確認できますErrorException

if ($e instanceof ErrorException) {
    echo $e->getSeverity();
} else {
    // no getSeverity() available
}
于 2012-05-29T19:08:01.933 に答える