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
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:
- 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){
……
}
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 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, namelyset_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.
-
<pre class='brush:php;toolbar:false;'>举例 register_shutdown_function(&#39;shutdown&#39;);function shutdown(){ if ($error = error_get_last()) { var_dump($error); } }$name //没写 ; 号</pre>Execution result
Emmmmm This is not nonsense ? Is it clearly not executed? 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools






