抛出自定义异常 PDO

Throw custom exception PDO

有没有办法设置 Pdo 对象来抛出自定义异常而不是默认的 PDOException?

例如:

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");
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 看看你如何通过参数路径。