1

Is there a way to setup a Pdo object to throw a custom exception instead of the default PDOException?

For example:

class MyCustomDbException extends PDOException{}

$pdo = new Pdo("mysql:host=localhost;dbname=myapp", "user_name", "secret_password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EXCEPTION_CLASS, "MyCustomDbException");
CarlosCarucce
  • 3,420
  • 1
  • 28
  • 51

1 Answers1

2
try {
    // Code is here
} catch (PDOException $e) {
    // See exception manual if you want to path through message or anything else from pdo exception.
    throw new YourException('PDO exception was thrown');
}

http://php.net/manual/en/language.exceptions.extending.php to see how you can path through parameters.

E_p
  • 3,136
  • 16
  • 28