php 异常处理

查看:74
本文介绍了php 异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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以异常的方式抛出,而不是警告。

mysqli_report(MYSQLI_REPORT_STRICT); // 加上这一行!!!

try{
    echo 'try <br/>';
    $mysql = new mysqli('localhost', 'root', '111', 'test');
} catch (Exception $e){
    echo 'catch <br/>';
    echo $e->getMessage();
}

再次访问,结果如下:
try
catch
Access denied for user 'root'@'localhost' (using password: YES)

这篇关于php 异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆