PHP: custom error handler for PDO?

I am trying to do something useful with PDO exceptions, except to display them, but I cannot find for myself how to use the error handler (set_error_handler) or anything normal to handle PDO exceptions.

Now I use try .. catch blocks, of course, a catch exception, I implement my own error handler in the catch part or I can completely skip the try-> catch block, since the exception handler would handle this for me (by calling or throw new exception (...) in the instructions?

I assume that I am asking for an example to catch PDO exceptions and register them more or less (any simple code is correct or not, I can use, I'm not too dumb).

+6
php exception-handling pdo error-handling
source share
1 answer

You will need to use a try..catch block for each PDO request. Add the registration function to the catch part. There is no general exception handler in PHP.

 try { pdo::error(); } catch (Exception $e) { syslog($e); } 

If you want to avoid try..catch blocks, you can configure PDO to only display errors instead of throwing exceptions. http://php.net/manual/en/pdo.error-handling.php

  $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); 
+10
source share

All Articles