Home >Backend Development >PHP Tutorial >Summary of PHP error and exception handling_PHP tutorial

Summary of PHP error and exception handling_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:12862browse

通过日志记录功能,你可以将信息直接发送到其他日志服务器,或者发送到指定的电子邮箱(或者通过邮件网关发送),或者发送到操作系统日志等,从而可以有选择的记录和监视你的应用程序和网站的最重要的部分。
错误报告功能允许你自定义错误反馈的级别和类型,可以是简单的提示信息或者使用自定义的函数进行处理并返回信息.

为什么要使用错误处理?
1.是网站出错时对用户友好
2.更好的避免错误、调试、修复错误
3.避免一些安全风险
4.更好保证程序的健壮性
5.……
一、最简单的错误处理――die()
当我们预计有错误发生时,停止脚步的运行。比如连接数据库时:

复制代码 代码如下:

mysql_connect('localhost', 'root', '123456') or die ('连接数据库错误:'. mysql_error());

不过,简单地终止脚本并不总是恰当的方式。
二、自定义错误和错误触发器
我们创建一个错误处理专用函数,使用set_error_handler函数设置后,可以在 PHP 中发生错误时调用该函数。

1.定义错误处理函数的参数:

参数 描述
error_level 必需。为用户定义的错误规定错误报告级别。必须是一个值数。

     参见下面的表格:错误报告级别。

error_message 必需。为用户定义的错误规定错误消息。
error_file 可选。规定错误在其中发生的文件名。
error_line 可选。规定错误发生的行号。
error_context 可选。规定一个数组,包含了当错误发生时在用的每个变量以及它们的值。

2. Error basic predefined constants:
Value Constant Description Remarks
1 E_ERROR (integer) Fatal runtime error. This type of error is generally an unrecoverable situation, such as a problem caused by memory allocation. The consequence is that the script terminates and does not continue to run.
2 E_WARNING (integer) Runtime warning (non-fatal error). Only a prompt message is given, but the script does not terminate.
4 E_PARSE (integer) Compile time syntax parsing error. Parsing errors are generated only by the parser.
8 E_NOTICE (integer) Runtime notification. Indicates that the script encounters a situation that may appear as an error, but there may also be similar notifications in scripts that can run normally.
16 E_CORE_ERROR (integer) Fatal error occurred during PHP initialization startup. This error is similar to E_ERROR, but is generated by the PHP engine core. since PHP 4
32 E_CORE_WARNING(integer) Warning (non-fatal error) that occurred during PHP initialization startup. Similar to E_WARNING, but generated by the PHP engine core. since PHP 4
64 E_COMPILE_ERROR(integer) Fatal compile-time error. Similar to E_ERROR, but generated by the Zend script engine. since PHP 4
128 E_COMPILE_WARNING(integer) Compile-time warning (non-fatal error). Similar to E_WARNING, but generated by the Zend script engine. since PHP 4
256 E_USER_ERROR (integer) User-generated error message. Similar to E_ERROR, but it is generated by the user using the PHP function trigger_error() in the code. since PHP 4
512 E_USER_WARNING(integer) User-generated warning message. Similar to E_WARNING, but it is generated by the user using the PHP function trigger_error() in the code. since PHP 4
1024 E_USER_NOTICE(integer) Notification messages generated by users. Similar to E_NOTICE, but it is generated by the user using the PHP function trigger_error() in the code. since PHP 4
2048 E_STRICT (integer) Enable PHP's suggested modifications to your code to ensure optimal interoperability and forward compatibility. since PHP 5
4096 E_RECOVERABLE_ERROR(integer) 可被捕捉的致命错误。 它表示发生了一个可能非常危险的错误,但是还没有导致PHP引擎处于不稳定的状态。 如果该错误没有被用户自定义句柄捕获 (参见 set_error_handler()),将成为一个 E_ERROR 从而脚本会终止运行。 since PHP 5.2.0
8192 E_DEPRECATED (integer) 运行时通知。启用后将会对在未来版本中可能无法正常工作的代码给出警告。 since PHP 5.3.0
16384 E_USER_DEPRECATED(integer) 用户产少的警告信息。 类似E_DEPRECATED, 但是是由用户自己在代码中使用PHP函数 trigger_error()来产生的。 since PHP 5.3.0
30719 E_ALL (integer) E_STRICT出外的所有错误和警告信息。 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously

(Levels E_ERROR and E_USER_ERROR cannot be caught by custom error handling functions) Fatal error messages cannot be caught in custom error functions because the script stops execution immediately when a fatal runtime error occurs.

3. Trigger errors
In the script where the user inputs data, it is useful to trigger an error when the user's input is invalid. In PHP, this task is accomplished by trigger_error().
You can trigger errors anywhere in the script. By adding a second parameter, you can specify the error level that is triggered.

4. Possible error types:

1).E_USER_ERROR - Fatal user-generated run-time error. The error cannot be recovered. Script execution was interrupted.
2).E_USER_WARNING - Non-fatal user-generated run-time warning. Script execution is not interrupted.
3).E_USER_NOTICE - Default. User-generated run-time notifications. The script found a possible error, which may have occurred while the script was running normally.
For example:

Copy code The code is as follows:

trigger_error("Something went wrong", E_USER_WARNING);
// Output Warning: Something went wrong in xxxx error message

3. Error reporting
By default, according to the error_log configuration in php.ini, PHP sends error records to the server's error recording system or file.
By using the error_log() function, you can send error records to a specified file or remote destination. For example, sending error messages to email is a good way.
For more error handling documentation, see: http://www.php.net/manual/zh/book.errorfunc.php

4. Exception handling
When an exception is thrown, the subsequent code will not continue to execute, and PHP will try to find a matching "catch" code block.
If the exception is not caught and there is no need to use set_exception_handler() for corresponding processing, then a serious error (fatal error) will occur and an "Uncaught Exception" error message will be output.
1. The processing procedure should include:

1.) try - Functions that use exceptions should be inside a "try" block. If no exception is triggered, the code continues execution as usual. But if an exception is triggered, an exception will be thrown.
2.) throw - This specifies how to trigger the exception. Each "throw" must correspond to at least one "catch"
3.) catch - The "catch" code block will catch the exception and create an object containing the exception information

2. Rethrow the exception

Sometimes, when an exception is thrown, you may want to handle it differently than the standard. The exception can be thrown again in a "catch" block.
The script should hide system errors from the user. System errors may be important to programmers, but users are not interested in them. To make it easier for users, you can throw the exception again with a user-friendly message.

3. Abnormal rules

1). Code that requires exception handling should be placed within a try code block to catch potential exceptions.
2). Each try or throw code block must have at least one corresponding catch code block.
3). Use multiple catch code blocks to catch different types of exceptions.
4).Exceptions can be re-thrown in the catch code block within the try code block.

In short: if an exception is thrown, you must catch it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736790.htmlTechArticleThrough the logging function, you can send information directly to other log servers or to a specified email address ( or sent through a mail gateway), or sent to the operating system...
Statement:
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