douxian1770 2017-02-20 10:28
浏览 101
已采纳

set_exception_handler尝试捕获错误

Here is the code I use to catch at the global level missed exceptions and errors:

set_exception_handler( function( Exception $e ) {
    $exceptionCode = $e->getCode();

    switch ( $exceptionCode ) {
        case '42000': # Database
            FlashMessages::flashIt( 'message', 'There is a syntax error in the db query' );
            include( Settings::ABSPATH . '/src/views/message.php' );
            var_dump( $e );
            exit;
            break;
        default:
            FlashMessages::flashIt( 'message', 'Something unpredicted happened.' );
            include( Settings::ABSPATH . '/src/views/message.php' );
            var_dump( $e );
            exit;
            break;
    }
} );

set_error_handler( function( $errno, $errstr, $errfile, $errline ) {
    FlashMessages::flashIt( 'message', 'An error happened.' );
    include( Settings::ABSPATH . '/src/views/message.php' );
    var_dump( $errstr );
    exit;
} );

What I expected: All exceptions caught by set_exception_handler. All errors caught by set_error_handler.

What I have: set_exception_handler tries to catch errors and generate problems: Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of Exception, instance of Error given

set_error_handler doesn't catch this kind of error even if I completely remove the set_exception_handler.

Question: how can I catch from those global functions the kind of error that is missed by both of these function ?

  • 写回答

1条回答 默认 最新

  • duangua5308 2017-02-20 10:46
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?