Home > Article > Backend Development > What to do if a global exception/error occurs in PHP
During development, we often need to customize error and exception handling functions to provide more friendly processing tips in different scenarios. Today we will introduce how to use set_exception_handler/set_error_handler to solve exceptions/errors. You can refer to it if necessary.
If we throw an Exception without try catch capture processing, the system will generate a fatal error and exit the execution after dumping the relevant information. .
throw new Exception("Error Processing Request", 1); Fatal error: Uncaught exception 'Exception' with message 'Error Processing Request'
set_exception_handler can set a global exception handling function. When the exception is not handled by try catch, the system will hand over the exception to this function for processing
<?php /** * 全局异常处理函数,会捕捉没有被 try catch 处理的异常 * @param [type] $exception [description] * @return [type] [description] */ function func_exception_handler($exception) { echo "User Exception:" . " [" . $exception->getCode() . "]" . " message " . $exception->getMessage() . " in file " . $exception->getFile() . " on line " . $exception->getLine() . PHP_EOL; } //如果不设定全局的异常处理函数 且 抛出异常时不使用 try catch 处理则会 fatal error set_exception_handler("func_exception_handler"); try { throw new Exception("我会被 try catch 捕捉处理,不影响执行流程!"); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; } throw new Exception("我没有被 try catch 捕捉处理,会被全局 set_exception_handler 处理!"); echo "没有被 try catch 处理的异常在我之前抛出,虽然被 set_exception_handler 但仍然会立刻退出执行,执行不到我哟" . PHP_EOL;
Note: However, because there is still no Exceptions are handled by try catch. After processing, the program will still exit execution and subsequent code will not be executed.
The errors we often encounter in PHP are: ERROR/WARNING/NOTICE
We can use the trigger_error function to trigger errors, and use set_error_handler to define ourselves error handling function.
trigger_error defaults to user-level NOTICE errors, which will not affect the execution process. The code will continue to execute. We can define the error level ourselves when triggered.
Note: set_error_handler is to intercept user-level errors. error, it does not allow the script to exit execution unless you manually implement error level judgment in your own processing code. If interception is not performed, user-level errors will also be handed over to the system's error handling mechanism. The system's error handling mechanism is to exit with an error and continue execution after a warning notice.
trigger_error("notice, go on!", E_USER_NOTICE); echo "executing!" . PHP_EOL; trigger_error("warning, go on!", E_USER_WARNING); echo "executing!" . PHP_EOL; trigger_error("error, exit!", E_USER_ERROR); echo "not execute!";
After customizing error handling, the error will no longer be passed to the system for processing
<?php /** * 用户自定义的错误处理 * @param [type] $err_no 错误级别 * @param [type] $err_msg 错误信息 * @param [type] $err_file 错误文件 * @param [type] $err_line 错误所在行 * @return [type] [description] */ function func_error_handler($err_no, $err_msg, $err_file, $err_line) { //trigger_error 默认触发的为 notice 级别的用户错误 $level = [ E_USER_ERROR => "Error", E_USER_WARNING => "Waring", E_USER_NOTICE => "Notice" ]; echo "User {$level[$err_no]}: " . " [" . $err_no . "]" . " message " . $err_msg . " in file " . $err_file . " on line " . $err_line . PHP_EOL; //如果需要 我们可以手动判断错误级别是否退出执行 if ($err_no == E_USER_ERROR) { exit("fatal error, exit!"); } } set_error_handler("func_error_handler"); trigger_error("notice, go on!", E_USER_NOTICE); trigger_error("warning, go on!", E_USER_WARNING); trigger_error("error, exit!", E_USER_ERROR);
Tips:
If an exception is thrown but does not use try catch processing, the system will generate an Fatal errors cause the script to exit execution. set_exception_handler only captures exceptions that are not handled by try catch. Customizing some friendly information output does not prevent fatal errors from occurring. The script will still exit execution.
set_error_handler will intercept the errors triggered by the user for processing instead of submitting them to the system, but it will not automatically identify the error level. We need to manually determine whether it is an ERROR level to exit, or a WARNING or NOTICE level prompts to continue execution.
Recommended learning: php video tutorial
The above is the detailed content of What to do if a global exception/error occurs in PHP. For more information, please follow other related articles on the PHP Chinese website!