CarlosCarucce
CarlosCarucce

Reputation: 3569

Throw custom exception PDO

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");

Upvotes: 1

Views: 955

Answers (1)

E_p
E_p

Reputation: 3144

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.

Upvotes: 2

Related Questions