Home>Article>Backend Development> Detailed explanation of error handling in PHP

Detailed explanation of error handling in PHP

WJ
WJ forward
2020-06-10 17:29:05 3436browse

Detailed explanation of error handling in PHP

Detailed explanation of PHP error handling

Error report

The general attribution of errors in PHP programs In the following three areas:

 1. Grammar errors

Grammar errors are the most common and easy to fix. For example: a semicolon is missing in the code. This type of error will prevent the execution of the script

 2. Runtime error

This kind of error will generally not prevent the execution of the PHP script, but will prevent what is currently being done. Output an error, but the php script continues to execute

 3. Logic error

This kind of error is the most troublesome. It neither prevents the script from executing nor outputs an error message

[Note] If display_errors in the php.ini configuration file is set from the default on to off, no errors will be displayed

In the PHP script, ini_set( can be called ) function, dynamically set the php.ini configuration file

ini_set("display_errors","On"); //Display all error messages

Detailed explanation of error handling in PHP

Error level

Detailed explanation of error handling in PHP

Actually, the 13 error types in the table can Divided into 3 categories: attention level, warning level and error level. Generally, during the development process, attention-level errors are ignored

"; getType();//未传入参数,警告级别 echo "222222222222222222222
"; getType3();//函数名错误,错误级别 echo "333333333333333333333
"; ?>

Detailed explanation of error handling in PHP

Error handling

1. The first error handling method is to modify Configuration file

The default error level is to prompt all levels of errors:error_reporting = E_ALL

Changeerror_reporting = E_ALLtoerror_reporting = E_ALL & ~E_NOTICEmeans not to prompt attention level errors. Then, restart the service to take effect

error_reporting = E_ALL & ~E_NOTICEThrow any unnoticed errors, the default valueerror_reporting = E_ERROR | E_PARSE | E_CORE_ERROROnly consider fatal ones Runtime errors, new parsing errors and core errorserror_reporting = E_ALL & ~(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE)Report all errors except user-caused errors

Detailed explanation of error handling in PHP

2. The second error handling method is to use the error handling function

In the PHP script, the error reporting level can be dynamically set through the error_reporting() function

"; getType();//警告级别 echo "222222222222222222222
"; getType3();//错误级别 echo "333333333333333333333
";?>

Detailed explanation of error handling in PHP

Customized Error Handling

Customize the way error reports are processed, which can completely bypass the standard PHP error handling function, so that you can print it in a format you define. Error report, or change the location where the error report is printed. You can consider customizing error handling in the following situations: 1. Write down error information and promptly discover some problems in the production environment; 2. Shield errors; 3. Control the output of errors. ; 4. As a debugging tool

Use the set_error_handler() function to set user-defined error handling

{$error_message}, 在文件{$error_file}中, 第{$error_line}行。
"; } getType($a); echo "1111111111111111
"; getType(); echo "222222222222222222222
"; echo "--------------------------------------------
"; echo $mess; ?>



Error log

Generally, programs will save error logs to record error information when the program is running. And the error log has its default storage location. We can modify the location of error information and error logs

In the PHP.ini configuration file, there are the following items that can be set for the error log

error_reporting = E_ALL//Every error will be sent to PHPdisplay_errors=Off//Do not display error reportlog_errors=On //Determine the location of the log statement recordlog_errors_max_log=1024//The maximum length of each log itemerror_log=G:/myerror.log//Specify the file where errors are written

In the php file, we can Use the function error_log() to customize error messages

Detailed explanation of error handling in PHP

Exception handling

Exception (Exception) handling is used to change the normal behavior of the script when a specified error occurs. Process is a new important feature in PHP5. Exception handling is an scalable and easy-to-maintain error handling mechanism, and provides a new object-oriented error handling method

try{
使用try去包含可能会发生异常的代码
一旦出现异常try进行捕获异常,交给catch处理。
抛出异常语句:throw 异常对象。
}catch(异常对象参数){
在这里做异常处理。
}[catch(。,,){
.. .. ..
}]

getMessage()." "; //输出捕获的异常消息 } echo 'Hello World'; //程序没有崩溃继续向下执行?>

自定义异常

  用户可以用自定义的异常处理类来扩展PHP内置的异常处理类。以下的代码说明了在内置的异常处理类中,哪些属性和方法在子类中是可访问和可继承的

  [注意]如果使用自定义的类来扩展内置异常处理类,并且要重新定义构造函数的话,建议同时调用parent::__construct()来检查所有的变量是否已被赋值。当对象要输出字符串的时候,可以重载__toString() 并自定义输出的样式

code."]:".$this->message."
"; } public function customFunction() { //为这个异常自定义一个处理方法 echo "按自定义的方法处理出现的这个类型的异常
"; } }?>
customFunction(); //通过自定义的异常对象中的方法处理异常 } echo '你好呀'; //程序没有崩溃继续向下执行?>

相关参考:php教程

The above is the detailed content of Detailed explanation of error handling in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51dev.com. If there is any infringement, please contact admin@php.cn delete