Home > Backend Development > PHP Problem > How to catch exceptions globally in php

How to catch exceptions globally in php

(*-*)浩
Release: 2023-02-23 15:58:01
Original
3676 people have browsed it

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 &#39;./main.php&#39;;<br/>} catch (\Throwable $e) {<br/>  // 执行自定义业务需求<br/>  var_dump($exception->getMessage());<br/>}<br/>
Copy after login

PHP 7 and below versions use set_error_handler to catch exceptions

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

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template