Home  >  Article  >  Backend Development  >  How to catch exceptions globally in php

How to catch exceptions globally in php

(*-*)浩
(*-*)浩Original
2019-09-04 14:50:173631browse

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/>

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/>

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!

Statement:
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