PHP error handling mechanism

Source: Internet
Author: User
This article mainly introduces the relevant information about the PHP error handling mechanism, which is very detailed and practical. if you need it, you can refer to it in the daily project development process, there will always be some unexpected exception errors. if we haven't done well with this, then the program looks very unprofessional and may become an effective information for others to attack the system; some errors cause termination of script execution. if there is no error message at this time, we can only read the code from the beginning, you need to know how terrible it is for us to have hundreds of lines of code in the project. how can we quickly and accurately locate exceptions and errors in the project development process, this article describes how to handle errors and exceptions. I will share with you how to learn from each other and make a memo.

System Error Processor:

Under normal conditions, PHP outputs errors normally. However, in some frameworks, the output may be affected. The framework may have its own processing mechanism, it may also be processed in the code. these function settings are generally used:

1. error_reporting (); set the PHP error level and return the current level

Error_reporting (report_level)

If the level parameter is not specified, the current error level is returned. The following items are possible values of level:

Tip: If this function is used, it will completely bypass the standard PHP error handler function. if necessary, the user-defined error handler must terminate the (die () script,

Note: If an error occurs before the script is executed, the custom error handler will not be used because the custom program has not been registered at that time.

The test code is as follows:

/***** @ Param type $ error_level error level * @ param type $ error_message error message * @ param type $ error_file optional error file * @ param type $ error_line optional error line *@ param type $ error_context is optional. Define an array that contains each variable and their values when an error occurs. */Function my_error ($ error_level, $ error_message, $ error_file, $ error_line, $ error_context) {echo date ('Y-m-d H: I: s '). $ error_level. $ error_message. $ error_file. $ error_line; var_dump ($ error_context);} set_error_handler ('My _ error', E_ALL); print_r ($ );

// The above case shows that when the my_error method is registered, the system will automatically overwrite the original error handling error_fuction () method.

Custom error trigger

Definition and usage

The trigger_error () function creates a user-defined error message.

Trigger_error () is used to trigger an error message under the conditions specified by the user. It is used together with the built-in error processor, or with user-defined functions created by the set_error_handler () function.

If an invalid error type is specified, the function returns false; otherwise, the function returns true.

Syntax

Trigger_error (error_message, error_types)

The test code is as follows:

/***** @ Param type $ level * @ param type $ msg */function my_error ($ level, $ msg) {switch ($ level) {case E_USER_ERROR: echo "ERROR:
"; Break; case E_USER_WARNING: echo" WARNING:
"; Break; case E_USER_NOTICE: echo" NOTICE:
"; Break; default: break;} echo" error code: ". $ level ."
"; Echo" error message :". $ msg;} // register the error Processor set_error_handler ('My _ error'); if (89> 8) {// call the error trigger trigger_error ('This is an error ', e_USER_WARNING );}

The running result is as follows:

WARNING:
Error No.: 512
Error message: this is an error.

The above is all the content of this article. I hope you will like it.

For more articles about the PHP error handling mechanism, refer to the PHP Chinese website!

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.