php捕捉錯誤的方法:1、使用“try catch”語句捕獲錯誤;2、通過“set_exception_handler”方法捕獲錯誤,該方法用于設(shè)置用戶自定義的異常處理函數(shù)。
php錯誤捕獲處理
一般捕獲錯誤使用的方法是:
try{...}catch(exception $e){echo $e->getmessage();}或者
set_exception_handler(function ($exception) {echo $exception->getmessage();});示例:
<?phpfunction test(){ throw new exception('參數(shù)錯誤');}try{ //如果catch沒有捕獲到,才會執(zhí)行到這里 set_exception_handler(function ($exception) { echo $exception;//exception 'exception' with message '參數(shù)錯誤' in /www/web/...(一堆信息) echo '<br>'; echo $exception->getmessage();//參數(shù)錯誤 }); test();}catch(exception $e){ echo $e->getmessage();//參數(shù)錯誤}set_exception_handler — 設(shè)置用戶自定義的異常處理函數(shù),用于沒有用 try/catch 塊來捕獲的異常。
更多相關(guān)知識,請訪問php中文網(wǎng)!