Detailed explanation of errors and exceptions in PHP and related knowledge

青灯夜游
Release: 2023-04-09 11:14:01
forward
2326 people have browsed it

Detailed explanation of errors and exceptions in PHP and related knowledge

PHP error level

Parse error>Fatal Error>Waning>Notice>Deprecated

  • ##Deprecated The lowest level error (not recommended, not Suggestion)


    It will appear when using some expired functions, and the program continues to execute

  • Notice notification level error


    Use some undefined It will appear when variables, constants or array keys are not quoted, and the program will continue to execute

  • Waning warning level error


    There is a problem with the program and it needs to be modified Code! ! ! The program continues to execute

  • Fatal Error Error level error


    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

  • Parse error Syntax parsing error


    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

  • E_USER_related errors


    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

  • In addition to setting in the script, you can also set it in php.ini Configure in the configuration file

  • 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

    ##PHP exceptions are a newly added feature. Different from JAVA/C# exceptions, PHP exceptions need to be thrown manually
  • throw new Exception

    instead of being automatically thrown by the system

  • The difference between PHP errors and exceptions, they are two
  • different concepts

    , but they have something in common:

    If the exception is not caught and handled, the program will terminate , and report a Fatal Error. When you see this, everyone will think that the exception is a kind of error. This is an illusion, but it can be understood this way. However, the program can continue to execute after the exception is caught, but the program must terminate after the real Fatal Error occurs.


  • Exceptions can be handled using
  • try{}catch( ){}

    to capture the capture. After the capture, subsequent code can continue to execute; and errors cannot be captured usingtry{}catch(){}

  • If an exception is thrown, it must be caught, otherwise the program will terminate execution.

PHP exception and error throwing

    Exception throwing:
  • throw new Exception('Some Error Message');

  • Error throw:
  • trigger_error()

  • trigger_error()

    Triggered errors will not be captured bytry-catchexception catching statements

##PHP error handling

##set_error_handler()
  • can only handle

    Deprecated
  • ,
Notice

,WaningThese three levels of errors, and after processing, the script will continue to execute the line after the error

register_shutdown_function()
  • 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()
    Notes
  • set_exception_handler(“myException”) not only accepts

    function name
  • ,
class methods

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'); ?>
Copy after login
PHP exception classification

Exception caused by user behavior

  • 1. Failure to pass the validator

    2、没查询到结果

    3、需要向用户返回具体信息

    4、不需要记录日志

    5、可作为异常或者不作为异常,根据需求和个人情况而定

  • 由于服务器自身导致出现异常

    1、代码出错

    2、调用第三方接口错误

    3、不需要向用户返回具体信息

    4、需要记录日志

在程序中PHP异常的自动抛出

  • 由于PHP异常是后面版本新增的特性,设计上与JAVA/C#的异常不一样,JAVA的异常大部分是系统自动抛出,而PHP异常不是系统自动抛出,需要手动抛出导致PHP异常在程序中的作用减半(异常就是意料之外的事情,根本我们意料不到的,如果用手动抛出,证明已经预先预料到了,那异常的意义就变味了)

  • 在PHP中异常是手动抛出的,而错误是系统自动抛出的(也可手动抛)

  • 我们需要把异常做成系统自动抛出接(例如JAVA)就必须借助错误(这三种错误DeprecatedNoticeWaning,其他的错误不行,因为会终止程序运行)

 getMessage() . '
'; } echo "end"; ?>
Copy after login

执行结果:

Division by zero end
Copy after login

PHP7 异常处理的大变化

  • 一段TP5源代码引出PHP7异常的变化

    Detailed explanation of errors and exceptions in PHP and related knowledge

    明明set_exception_handler()函数只可以捕获Exception类或派生类的对象,为何还需要捕获的对象做判断呢?结果引出了PHP7的变化,请看下面分析

  • 前面已经讲过异常是需要手动抛出,及时上面所说的方法最多也是把DeprecatedNoticeWaning这3类错误封装成系统自动抛出的异常,但致命错误仍然还是无法封装成系统自动抛出的异常,因为致命错误(Fatel Error)仍然无法捕获

  • 在PHP7之前,DeprecatedNoticeWaning这类错误是可以捕获的(使用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){ //捕获错误 }
Copy after login

相关教程推荐:《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!

Related labels:
source:segmentfault.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!