时间:2021-07-01 10:21:17 帮助过:35人阅读
php捕捉错误的方法:1、使用“try catch”语句捕获错误;2、通过“set_exception_handler”方法捕获错误,该方法用于设置用户自定义的异常处理函数。

PHP错误捕获处理
一般捕获错误使用的方法是:
try{
...
}catch(Exception $e){
echo $e->getMessage();
}或者
set_exception_handler(function ($exception) {
echo $exception->getMessage();
});示例:
<?php
function test(){
throw new Exception('参数错误');
}
try{
//如果catch没有捕获到,才会执行到这里
set_exception_handler(function ($exception) {
echo $exception;//exception 'Exception' with message '参数错误' in /www/web/...(一堆信息)
echo '<br>';
echo $exception->getMessage();//参数错误
});
test();
}catch(Exception $e){
echo $e->getMessage();//参数错误
}set_exception_handler — 设置用户自定义的异常处理函数,用于没有用 try/catch 块来捕获的异常。
更多相关知识,请访问PHP中文网!
以上就是php如何捕捉错误的详细内容,更多请关注gxlcms其它相关文章!