First of all, let me state that the author’s PHP version is 7.2
What is exception?
For example, if you connect to the database and all the parameters are written, but you find that the link cannot go up, this is not as expected.
Can be captured by try-catch
What is an error?
For example:
Exception handling
So if you want to catch an exception in the future, and you don’t know whether the exception is an Error or an Exception, you can throw it like this
try{ …… }catch(Throwable $e){ …… }
Error levels
Errors in php also have levels
Parse error
>Fatal Error >
Waning >
Notice >
Deprecated
Deprecated 最低级别的错误(不推荐,不建议) 使用一些过期函数的时候会出现,程序继续执行 Notice 通知级别的错误 使用一些未定义变量、常量或者数组key没有加引号的时候会出现,程序继续执行 E_NOTICE // 运行时通知。表示脚本遇到可能会表现为错误的情况. E_USER_NOTICE // 用户产生的通知信息。Waning 警告级别的错误 程序出问题了,需要修改代码!!!程序继续执行 E_WARNING // 运行时警告 (非致命错误)。 E_CORE_WARNING // PHP初始化启动过程中发生的警告 (非致命错误) 。 E_COMPILE_WARNING // 编译警告 E_USER_WARNING // 用户产生的警告信息Fatal Error 错误级别的错误 程序直接报错,需要修改代码!!!中断程序执行,可使用register_shutdown_function()函数在程序终止前触发一个函数 E_ERROR // 致命的运行错误,错误无法恢复,暂停执行脚本 E_CORE_ERROR // PHP启动时初始化过程中的致命错误 E_COMPILE_ERROR // 编译时致命性错,就像由Zend脚本引擎生成了一个E_ERROR E_USER_ERROR // 自定义错误消息。像用PHP函数trigger_error(错误类型设置为:E_USER_ERROR)Parse error 语法解析错误 语法检查阶段报错,需要修改代码!!!中断程序执行,除了修改ini文件,将错误信息写到日志中,什么也做不了 E_PARSE //编译时的语法解析错误
Custom error handlerSometimes , the error handler that comes with PHP cannot fully meet our needs. Most of the time, we need to manually rewrite exception handling.
php provides us with three functions to help us handle them, namelyset_error_handler()
functions to host errors Handler, you can customize the error handling process.
For uncaught exception handling
Function: Register a function that will be executed when PHP terminates<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="prettyprint">举例
register_shutdown_function(&#39;shutdown&#39;);function shutdown(){
if ($error = error_get_last()) {
var_dump($error);
}
}$name //没写 ; 号</pre><div class="contentsignin">Copy after login</div></div>Execution resultParse error: syntax error, unexpected ';' in /app/swoole/errorDemo.php on line 34</p>
<p> <br> Emmmmm This is not nonsense ? Is it clearly not executed? <code>
In fact, the reason is that before the program is executed, our PHP will first check the syntax problems of our program. If there are no problems, we can execute our program. The above is the detailed content of Detailed explanation of exception and error handling in php7. For more information, please follow other related articles on the PHP Chinese website!