php捕捉错误的方法:1、使用“try catch”语句捕获错误;2、通过“set_exception_handler”方法捕获错误,该方法用于设置用户自定义的异常处理函数。
php错误捕获处理
一般捕获错误使用的方法是:
try{...}catch(exception $e){echo $e->getmessage();}或者
set_exception_handler(function ($exception) {echo $exception->getmessage();});示例:
<?phpfunction 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中文网!