Better handling of PHP errors

Source: Internet
Author: User
Tags php error

===== error Type = = =

There are two main types of errors in PHP: triggering errors and exceptions.

Where the triggering error can be divided into: Compile errors, engine errors and run-time errors, the first two are not captured;

Exceptions can be captured, and the code is interrupted when no attempt is attempted to capture it.

Trigger errors can be obtained by Error_get_last (), and exceptions can be captured using standard try...catch statements.


===== Treatment Method =====

"Error Reporting"

The error_reporting () function is a function that PHP provides to control the level of error, and if the corresponding error level is set, only the error message of the response will be output on the page (or output, for the command line) when the corresponding error level is triggered.

The error_reporting (int level) support levels parameter uses a binary number to configure the error level, and the error level generally replaces the corresponding binary identifier with a constant. For example: E_all, E_notice, E_user_notice, and so on; If the parameter is not passed, the current error level is returned (typically used to hold the current error level for subsequent recovery).

Where E_parse, e_compile_*, and e_core_* are non-run-time errors that cannot be captured


"PHP.ini"

The configuration file is the default configuration for PHP initialization, and the corresponding initialization parameters can be controlled by modifying some parameters. If you want to control errors, you can configure them in several ways:

Display_errors-whether an error is displayed, typically in a production environment it is recommended to close the parameter and try it together with the following parameters log_errors-Log the error switch if Open will be recorded to the corresponding location Error_log-the location of the error log is not specified as the system default error _reporting-the same function as above


"Error Suppression"

PHP provides a special operation to suppress an error by adding a suppress error operator @ before the statement. This is typically used when you do not know what will happen, such as opening an indeterminate file or a Web URL, but it is generally not recommended

Suppress the error itself within a series of processing to achieve this effect, and for the error itself is completely unknown, in general, it is not recommended to use


"Catch Exception"

In almost all languages, there is a handling of exceptions. If the program throws an exception, you can catch the exception through the Try...catch statement, or you can only catch errors of that type if you explicitly know the type of the error. Try...catch is one of the most standard error handling methods.

try {

Code section

} catch (Exception $e) {

Processing $e against an exception object

}


"Set Error Handler"

PHP provides the Set_error_handler () function to allow developers to pass in a function name (or anonymous function, for PHP 5.2 or more) to get all the errors into the function, and then the developer can control within the function, for example

Set_error_handler (function ($code, $msg) {

Log_error ("$msg occur with code $code");

});

Correspondingly, PHP also provides restore_error_handler () to revert to the standard PHP error control.


"Set Exception handler"

If every exception is captured, it is obviously a complex thing to write a lot of code, and it will be more complex to maintain, and for a fast programming language, PHP will not allow this to happen naturally. Using Set_exception_handler (), you can receive all the uncaught exceptions like the above error-handling controller, and then do something that you can do, and even throw out the errors. For example:

Set_exception_handler (function ($exception) {

Log_error ("Exception occur: $exception");

});


"Crash capture"

This kind of processing is not the PHP standard processing method, but for some kind of failure to make a program can not catch the error is very useful, it refers to the registration of a shutdown function, within the function by calling Error_get_last () to obtain the final error, and according to the error level to do the corresponding processing.

Register_shutdown_function (function () {

$error = Error_get_last ();

if ($error && $error [' Code '] () () & Error_reporting ()) {

Crash error, logging

}

});


This article is from the "Half City" blog, please be sure to keep this source http://vabc1314.blog.51cto.com/2164199/1854207

Better handling of PHP errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.