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.

2
  • I think it's because PDO uses its own Exception handle class. Commented Jun 2, 2013 at 3:10
  • @hjpotter92 : so how can I catch the PDOexcepion with a custom exception handler ? Commented Jun 2, 2013 at 3:14

1 Answer 1

1

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

If it's full and complete error message you have - then there is some your own code ( most likely a global try..catch) that prints the caught error out.
(If not - please, ask question properly, providing full and complete error message - so, we'll be able to help you out.)

Sign up to request clarification or add additional context in comments.

1 Comment

It was surrounded by another global try catch which I was forgot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.