Home > Backend Development > PHP Tutorial > Detailed explanation of PHP exception handling, error reporting, and logging

Detailed explanation of PHP exception handling, error reporting, and logging

PHP中文网
Release: 2023-02-28 18:36:01
Original
1187 people have browsed it

In php we often encounter some errors to deal with. Let me summarize the exception handling, error reporting, and some logs in php Content summary and introduction.

Error handling:

1.Syntax error

2. Runtime error

3. Logic error

Error report:

Error: E_ERROT The program will be interrupted, an error occurred

Warning: E_WARNING The program will not be interrupted, but some functions may not be implemented

Note: E_NOTICE will not affect the program and can be completely blocked

Output all error reports during development and disable all error reports during runtime

Write errors to the log:

1. Turn on the log (error_log = On in php.ini), and turn off error reporting. The error (if it occurs, but direct output is not allowed) will be recorded in the log

2 .If the log path is not specified, it will be written to the web server log by default

Set error report:

error_reporting(E_ALL) //输出所有报告
Copy after login

Modify php.iniConfiguration file :

ini_set(“display_errors”,off) //修改为不显示错误报告
ini_get(“upload_max_filesize”) //读取配置文件中上传文件大小限制
Copy after login

Accidents are unexpected things that happen while the program is running. Use exceptions to change the normal flow of the script. Exception handling:

PHP 5 provides a new Object-oriented error handling method.

Exception handling is used to change the normal flow of a script when a specified error (exception) situation occurs. This situation is called an exception.

When an exception is triggered, what typically happens is:

• The current code state is saved
• Code execution is switched to predefined exception handling ProcessorFunction
•Depending on the situation, the processor may restart code execution from the saved code state, terminate script execution, or continue script execution from another location in the code
We will show Different error handling methods:

•ExceptionBasic use
•Create custom exception handler
•Multiple exceptions
•Re Throw an exception
•Set the top-level exception handler

Syntax:

try{
可能出错的代码
throw new Exception(“异常信息”)
}catch(Exception $e[异常对象]){
后面的正常代码
}
Copy after login

Example

function   runtimeErrorHandler($level,$string) 
{
//自定义错误处理时,手动抛出一个异常实例
//为了把错误级别代码也显示出来,这里拼接了错误代码和错误信息作为新的错误信息来传递。
throw   new   Exception($level.'|'.$string); 
} 
//设置自定义错误处理函数
set_error_handler( "runtimeErrorHandler");
try
{
$a=2/0;  
//这里制造一个以前无法截获的除0错误
}
catch(Exception $e)
{
echo '错误信息:', $e->getMessage();
//显示错误,这里就可以看到错误级别和错误信息了“2|Division by zero”
}
Copy after login

2. If try If there is an exception in the code, an exception object will be thrown. If $e is captured in catch(), it will point to the exception object. Then continue to execute 1. If there is no exception in the code in try, it will execute normally.

3.$e->getMessage() gets exception information

Custom exception class:

Function: Write some methods to solve specific exceptions (Built-in classes have no processing methods)

1. Custom exception class must be a subclass of Exception (built-in class)

2. There are only constructors in the Exception class and toString() can be rewritten

3. Define the required methods

Exception rules

• Code that needs exception handling should be placed in the try code block to catch potential exceptions.
•Every try or throw block must have at least one corresponding catch block.
•Use multiple catch blocks to catch different kinds of exceptions.
•Exceptions can be re-thrown in a catch block within a try block.

The above is the detailed explanation of PHP exception handling, error reporting, and logs. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

http://www.bkjia.com/PHPjc/633087.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633087.htmlTechArticle we often encounter in php Some errors need to be handled. Let me summarize and introduce some exception handling, error reporting, and logs in PHP. Error handling: 1. Syntax error...


Related labels:
source:php.cn
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template