【PHP】 捕获全局异常

PHP 7 以下版本使用 set_error_handler 捕获异常

<?php
error_reporting(E_ALL);
set_error_handler('handle_error');
function handle_error($no,$msg,$file,$line){
 // 执行自定义业务需求
}
try {
  require_once './main.php';
} catch (\Exception $exception) {
  // 执行自定义业务需求
} catch (\Error $error) {
  // 执行自定义业务需求
}

PHP 7 以上版本使用 Throwable 捕获异常

<?php
 // 关闭所有错误信息
 error_reporting(E_ALL);
 
try {
 // main.php 为实际业务场景下入口文件
 require_once './main.php';
 } catch (\Throwable $e) {
   // 执行自定义业务需求
   var_dump($exception->getMessage());
}

posted @ 2020-10-09 19:21  依然范儿特西  阅读(293)  评论(0编辑  收藏  举报