try{
echo 'try <br/>';
$mysql = new mysqli('localhost', 'root', '111', 'test');
} catch (Exception $e){
echo 'catch <br/>';
echo $e->getMessage();
}
代码如上, 出现了异常
输出结果:
try
Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /home/test/its2/webroot/public/unserialize.php on line 31
为什么没有执行输出catch?
你需要告诉mysqli以异常的方式抛出,而不是警告。
再次访问,结果如下:
try
catch
Access denied for user 'root'@'localhost' (using password: YES)