我正在构建一个自定义异常类来管理所有异常:
class MyExceptions extends Exception
{
public function __construct($message = 'Unkown errror', $code = -1, Exception $previous = null) {
echo 'init!';
parent::__construct($message, $code, $previous);
}
}
现在,当 PDOException 发生时,我想将其重新抛出到 MyExceptions:
class myDB
{
private $db;
public function dbConnect() {
try {
$this->db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
}
catch (PDOException $e) {
throw new MyExceptions($e);
}
/* Updated */
catch (MyExceptions $e) {
echo 'caught!';
}
}
}
问题是当数据库连接异常出现时,屏幕上出现以下致命错误:
init!
Fatal error: Uncaught exception 'MyExceptions' with message...
因此,尽管调用了 MyExceptions __construct()(请参见显示的“init!”),但不会捕获异常。我读到的每一点资源都指向我的确切实现,我不知道我做错了什么。