PHP error level
Parse error
>Fatal Error
>Waning
>Notice
>Deprecated
It will appear when using some expired functions, and the program continues to execute
Use some undefined It will appear when variables, constants or array keys are not quoted, and the program will continue to execute
There is a problem with the program and it needs to be modified Code! ! ! The program continues to execute
The program reports an error directly and the code needs to be modified! ! ! To interrupt program execution, you can use the register_shutdown_function() function to trigger a function before the program terminates
An error is reported during the syntax check phase and needs to be modified Code! ! ! Interrupting program execution, nothing can be done except modifying the ini file and writing error messages to the log
User-defined Error, the user manually throws the error and performs customized error handling
PHP error related functions
ini_set('display_errors', 0);//Turn off error output (development environment is on, production environment is off)
error_reporting(E_ALL&~E_NOTICE);//Set the error reporting level
ini_set('error_reporting',0);//Set the error reporting level
PHP error configuration
error_reporting = E_ALL&~E_NOTICE;//Set the error reporting level
display_errors = 1;//Open the development environment and close the production environment
PHP exception
instead of being automatically thrown by the system
, but they have something in common:
to capture the capture. After the capture, subsequent code can continue to execute; and errors cannot be captured usingtry{}catch(){}
PHP exception and error throwing
Triggered errors will not be captured bytry-catch
exception catching statements
##set_error_handler()
can only handle
,Waning
These three levels of errors, and after processing, the script will continue to execute the line after the error
This method is the last callback function before the end of the script, so it will be called whether it is die()/error (exception)/or the script ends normally
PHP exception handling
set_exception_handler()
Set the default exception handler. If there is a try/catch capture, this function will not be executed. Otherwise, it will be executed. And if it is executed, the script will not continue to execute the next line of code where the exception occurs, and the program will terminate immediately
set_exception_handler(“myException”) not only accepts
can also be accepted (public static methodsandpublic non-static methodsare acceptable), but they need to be inarray formPass, the first value of the array is "class name", and the second parameter is "method name", as shown in the following code:
Exception: " , $exception->getMessage(); } } set_exception_handler(array('App','myException')); throw new Exception('Uncaught Exception occurred'); ?>
Exception caused by user behavior
2、没查询到结果
3、需要向用户返回具体信息
4、不需要记录日志
5、可作为异常或者不作为异常,根据需求和个人情况而定
由于服务器自身导致出现异常
1、代码出错
2、调用第三方接口错误
3、不需要向用户返回具体信息
4、需要记录日志
在程序中PHP异常的自动抛出
由于PHP异常是后面版本新增的特性,设计上与JAVA/C#的异常不一样,JAVA的异常大部分是系统自动抛出,而PHP异常不是系统自动抛出
,需要手动抛出
,导致PHP异常在程序中的作用减半
(异常就是意料之外的事情,根本我们意料不到的,如果用手动抛出,证明已经预先预料到了,那异常的意义就变味了)
在PHP中异常是手动抛出的
,而错误是系统自动抛出的
(也可手动抛)
我们需要把异常做成系统自动抛出接
(例如JAVA)就必须借助错误
(这三种错误Deprecated
、Notice
、Waning
,其他的错误不行,因为会终止程序运行)
getMessage() . '
'; } echo "end"; ?>
执行结果:
Division by zero end
PHP7 异常处理的大变化
一段TP5源代码引出PHP7异常
的变化
明明set_exception_handler()函数只可以捕获Exception类或派生类的对象
,为何还需要捕获的对象做判断呢?结果引出了PHP7
的变化,请看下面分析
前面已经讲过异常是需要手动抛出
,及时上面所说的方法最多也是把Deprecated
、Notice
、Waning
这3类错误封装成系统自动抛出的异常,但致命错误仍然还是无法封装成系统自动抛出的异常,因为致命错误(Fatel Error)仍然无法捕获
在PHP7之前,Deprecated
、Notice
、Waning
这类错误是可以捕获的(使用set_error_handler()函数),而Fatel Error
无法捕获的
在PHP7之后,出现了一个异常与错误通用的接口Throwable,Exception类与Error类都实现了该接口,导致Error类或Error类的派生类的错误对象
(大部分Fatel Error,而之前三类错误不变)也可以像Exception一样被捕获(2种捕获方法:1、try/catch 2、set_exception_handler())
示例代码
try{ go();//该函数未定义 }catch(Exception $e){ //捕获异常 }catch(Error $er){ //捕获错误 }
相关教程推荐:《PHP教程》
The above is the detailed content of Detailed explanation of errors and exceptions in PHP and related knowledge. For more information, please follow other related articles on the PHP Chinese website!