模型类抛出异常后无法被捕获?
<?php namespace core\lib; class model extends \PDO{ public function __construct(){ $dsn = "mysql:host=localhost;dbname=test"; $username = "root"; $password = "wrongpw"; try{ parent::__construct($dsn,$username,$password); }catch(\PDOExpcetion $e){ echo $e->getMessage(); } } }
按照老师的代码,故意将数据库密码写错,执行时抛出的异常不能被捕获而报错,这是为什么呢?PS:密码不写错,则程序运行正常,model类可以正常执行CURD语句。
报错信息如下:
查资料看着是抛出的pdoexception对象未被捕获才会这样报错。
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in D:\xampp\htdocs\MoocFramework\FW\core\lib\model.php:13 Stack trace: #0 D:\xampp\htdocs\MoocFramework\FW\core\lib\model.php(13): PDO->__construct('mysql:host=loca...', 'root', '1234561') #1 D:\xampp\htdocs\MoocFramework\FW\app\ctrl\indexCtrl.php(11): core\lib\model->__construct() #2 D:\xampp\htdocs\MoocFramework\FW\core\imooc.php(37): app\ctrl\indexCtrl->index() #3 D:\xampp\htdocs\MoocFramework\FW\index.php(30): core\imooc::run() #4 {main} thrown in D:\xampp\htdocs\MoocFramework\FW\core\lib\model.php on line 13