3

I have a simple piece of code, using PDO in PHP:

$conn = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password, 
        array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_PERSISTENT => false,
    ));

and this custom exception handler:

function my_exceptionHandler($exception) {
    echo "Exception: {$exception->getMessage()}";
}
set_exception_handler("my_exceptionHandler");

Although the custom exception handler catches all the other exceptions, but it fails to catch the PDO-exceptions i.e. when the username and password for database are incorrect, and I just get the plain error:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) 

Is there anything I miss like a overloading custom exception handler function for this type of exceptions? And note that when I wrapped the PDO code in try and catch block, it worked fine but I want to catch it in my custom exception handler.

4

1 回答 1

1

SQLSTATE[HY000] [1045] 用户'root'@'localhost'的访问被拒绝(使用密码:YES)

如果您有完整且完整的错误消息 - 那么有一些您自己的代码(很可能是全局 try..catch)打印出捕获的错误。
(如果不是 - 请正确提问,提供完整和完整的错误消息 - 所以,我们将能够为您提供帮助。)

于 2013-06-02T04:21:28.023 回答