Home > Backend Development > PHP7 > body text

Detailed explanation of exception and error handling in php7

coldplay.xixi
Release: 2023-02-17 21:24:01
forward
2588 people have browsed it

Detailed explanation of exception and error handling in php7

First of all, let me state that the author’s PHP version is 7.2

    • ##Overview of exceptions and errors
        • What is exception?
        • What is an error?
        • Exception handling
    • Level of error
    • Custom error handler
        • set_error_handler()
        • set_exception_handler()
        • register_shutdown_function()
        • Framework error handling

Recommended (free): PHP7

##Overview of exceptions and errors

What is exception?

Exception refers to a situation that does not meet expectations and is different from the normal process during program operation.

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?

is a problem that belongs to the PHP program itself. It is usually caused by illegal syntax and environmental problems, which makes the compiler fail to pass the check or even fail to run.

The warming and notices we usually encounter are all errors, but they are of different levels.


For example:

TypeError (type error) The function parameter type I specified is inconsistent with the parameter passed in
  • ArithmeticError (arithmetic error)
  • ParseError (parsing error) In the loaded file, there is a syntax error in include "demo.php" or eval(); causing parsing failure
  • AssertionError (Assertion error) This error is generated when assert takes effect.
  • pisionByZeroError (the denominator is zero) During the operation, such as division, the denominator is 0
  • Except for these situations, the rest are all exceptions

Exception handling

In the previous php5. Throw the error in try-catch

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){
    ……
}
Copy after login

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  //编译时的语法解析错误
Copy after login
Custom error handler

Sometimes , 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, namely

set_error_handler()

functions to host errors Handler, you can customize the error handling process.

    If an error occurs in the code before this function, our custom processing function will not be called because it has not been registered yet
  • After setting this function, error_reporting() will be invalid
  • The following level errors cannot be handled by user-defined functions: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING. This function can only capture some of our Warning and Note level errors
  • set_exception_handler()

For uncaught exception handling

  • register_shutdown_function()

Function: Register a function that will be executed when PHP terminates

    Capture PHP errors: Fatal Error, Parse Error, etc. This method is the last function called before the PHP script execution ends, such as script errors, die(), exit, exception, and normal end will all be called.
  • If used for error handling, you need to cooperate with
  • error_get_last()
  • It can get the last error that occurred.
  • <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="prettyprint">举例 register_shutdown_function(&amp;#39;shutdown&amp;#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.

    Our above code did not pass our syntax check, so an error was reported directly.

    Then the question comes? When we are in the framework, why do the frameworks always report errors to us?

    Error handling of the framework

    In the framework, its code is loaded through an entry file. When our PHP detects syntax errors, we only check our index.php. If it requires, it will not be detected. When our code makes an error, it is an error detected in run-time, so the corresponding error in our framework can be

    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!

Related labels:
source:csdn.net
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