douren1891 2015-04-01 13:10
浏览 39
已采纳

PHP中的异常 - set_exception_handler输出

I'm starting to use set_exception_handler now. The first place I tested is the try/catch block for my PDO layer.

I forced an Exception with incorrect database credentials. (this is before I applied the

<?php  

function log_exception($exception){
    print_r($exception);    
}

set_exception_handler("log_exception");

try { 
    $dbh = new PDO();
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo "connected!! 
";
} catch (PDOException $e) { 
    print_r($e); // let's see what it looks like
    throw new Exception($e); 
}
?>

You'll see print_r inside the catch, as well as in the log_exception function.

This is what gets displayed from print_r($e):

PDOException Object
(
    [message:protected] => SQLSTATE[28000] [1045] Access denied for user 'localhost'@'127.0.0.1' (using password: YES)
    [string:Exception:private] =>
    [code:protected] => 1045
    [file:protected] => /var/www/html/test.php
    [line:protected] => 35
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /var/www/html/test.php
                    [line] => 35
                    [function] => __construct
                    [class] => PDO
                    [type] => ->
                    [args] => Array
                        (
                            [0] => mysql:host=127.0.0.1;dbname=db_tests;charset=utf8
                            [1] => test
                            [2] => test
                        )

                )

        )

    [previous:Exception:private] =>
    [errorInfo] =>
)

And this is what gets displayed from print_r($exception) inside the log_error() function:

Exception Object
(
    [message:protected] => exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'localhost'@'127.0.0.1' (using password: YES)' in /var/www/html/test.php:34
Stack trace:
#0 /var/www/html/test.php(34): PDO->__construct('mysql:host=127.0...', 'test', 'test')
#1 {main}
    [string:Exception:private] =>
    [code:protected] => 0
    [file:protected] => /var/www/html/test.php
    [line:protected] => 34
    [trace:Exception:private] => Array
        (
        )

    [previous:Exception:private] =>
)

What are they different? I am assuming that whatever Exception Object is passed into the log_error() function is going to be the same as what got generated within the catch.

Am I missing something?

...

  • 写回答

2条回答 默认 最新

  • dongzhong6675 2015-04-01 13:20
    关注

    You should be throwing the same exception if you want to forward it, not a new one:

    throw $e;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置