Home >Backend Development >PHP Problem >How to catch exceptions globally in php
PHP 7 and above versions use Throwable to catch exceptions
index.php: (Recommended learning: PHP video tutorial)
<?php<br/>// 关闭所有错误信息<br/>error_reporting(E_ALL);<br/><br/>try {<br/> // main.php 为实际业务场景下入口文件<br/> require_once './main.php';<br/>} catch (\Throwable $e) {<br/> // 执行自定义业务需求<br/> var_dump($exception->getMessage());<br/>}<br/>
PHP 7 and below versions use set_error_handler to catch exceptions
<?php<br/>error_reporting(E_ALL);<br/>set_error_handler('handle_error');<br/>function handle_error($no,$msg,$file,$line){<br/> // 执行自定义业务需求<br/>}<br/>try {<br/> require_once './main.php';<br/>} catch (\Exception $exception) {<br/> // 执行自定义业务需求<br/>} catch (\Error $error) {<br/> // 执行自定义业务需求<br/>}<br/>
The above is the detailed content of How to catch exceptions globally in php. For more information, please follow other related articles on the PHP Chinese website!