PHP Error and _php summary tutorial

Source: Internet
Author: User
Tags php error system log
With logging, you can send information directly to other log servers, either to a designated email address (or via a mail gateway), to an operating system log, and so on, so you can selectively record and monitor the most important parts of your application and website.
The error reporting feature allows you to customize the level and type of error feedback, which can be a simple hint or use a custom function to process and return information.

Why use error handling?
1. User friendly when site error occurs
2. Better avoid errors, debug, fix Errors
3. Avoid some security risks
4. Better guarantee the robustness of the program
5 .....
One, the simplest error handling ――die ()
When we anticipate an error to occur, stop the running of the footstep. For example, when connecting to a database:
Copy the Code code as follows:
mysql_connect (' localhost ', ' root ', ' 123456 ') or Die (' Connection Database error: '. mysql_error ());

However, simply terminating the script is not always the proper way.
Second, custom errors and error triggers
We create an error-handling special function that, when set with the Set_error_handler function, can be called when an error occurs in PHP.

1. Define parameters for error handling functions:

Parameters Description
Error_level Necessary. Specifies the error reporting level for user-defined errors. Must be a number of values.

See the table below: Error reporting levels.

Error_message Necessary. Specifies error messages for user-defined errors.
Error_file Optional. Specifies the file name in which the error occurred.
Error_line Optional. Specifies the line number where the error occurred.
Error_context Optional. Specifies an array that contains each variable that was used when the error occurred and their value.

2. Error basic pre-defined constants:
value Constants Description Notes
1 E_error (integer) Fatal run-time error. Such errors are generally unrecoverable situations, such as problems caused by memory allocations. The result is that the script terminates and no longer continues to run.
2 e_warning (integer) Run-time warning (non-fatal error). Only the prompt is given, but the script does not terminate the run.
4 E_parse (integer) Compile-time syntax parsing error. Parsing errors are generated only by the parser.
8 E_notice (integer) Run-time notifications. Indicates that the script encountered a situation that might behave as an error, but there may be similar notifications in a script that can run correctly.
16 E_core_error (integer) A fatal error occurred during PHP initialization startup. This error is similar to e_error, but is generated by the core of the PHP engine. Since PHP 4
32 e_core_warning (integer) PHP initialization warnings (non-fatal errors) that occur during startup. Similar to e_warning, but generated by the core of the PHP engine. Since PHP 4
64 E_compile_error (integer) Fatal compile-time error. Similar to E_error, but generated by the Zend scripting engine. Since PHP 4
128 e_compile_warning (integer) Compile-time warning (non-fatal error). Similar to e_warning, but generated by the Zend scripting engine. Since PHP 4
256 E_user_error (integer) User-generated error message. Similar to e_error, but is generated by the user himself 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 is generated by the user himself using the PHP function Trigger_error () in the code. Since PHP 4
1024 E_user_notice (integer) User-generated notification information. Similar to E_notice, but is generated by the user himself using the PHP function Trigger_error () in the code. Since PHP 4
2048 e_strict (integer) Enable PHP recommendations for code modifications to ensure the best interoperability and forward compatibility of your code. Since PHP 5
4096 E_recoverable_error (integer) A fatal error that can be captured. It indicates that a potentially very dangerous error has occurred, but has not yet caused the PHP engine to be in an unstable state. If the error is not captured by the user custom handle (see Set_error_handler ()), it becomes a e_error and the script terminates. Since PHP 5.2.0
8192 e_deprecated (integer) Run-time notifications. When enabled, warns you about code that might not work correctly in a future release. Since PHP 5.3.0
16384 e_user_deprecated (integer) The user produces less warning messages. Similar to e_deprecated, but is generated by the user himself using the PHP function Trigger_error () in the code. Since PHP 5.3.0
30719 E_all (integer) e_strict All error and warning messages that go out. 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously

(Level E_error, E_user_error cannot be caught by a custom error handler) The custom error function cannot catch a fatal error message because the script stops executing immediately when a fatal run-time error occurs.

3. Triggering errors
The location of the user input data in the script is useful for triggering errors when the user's input is not valid. In PHP, this task is done by Trigger_error ().
You can trigger an error anywhere in the script, and by adding a second parameter, you can specify the level of error that is triggered.

4. Possible types of errors:

1). E_user_error-Fatal user generated run-time error. The error cannot be recovered. The execution of the script was interrupted.
2). E_user_warning-A 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, or it might occur when the script is running normally.
For example:
Copy the Code code as follows:
Trigger_error ("Wrong ah", e_user_warning);
Output Warning: Error message in XXXX

Third, error Reporting
By default, PHP sends error records to the server's error logging system or file, based on the Error_log configuration in php.ini.
By using the Error_log () function, you can send an error record to a specified file or remote destination. For example, it is a good way to send the error message to the mailbox.
For more error handling documents see: http://www.php.net/manual/zh/book.errorfunc.php

Iv. Exception Handling
When the exception is thrown, the subsequent code does not continue, and PHP tries to find a matching "catch" block.
If the exception is not captured and does not use Set_exception_handler () for appropriate processing, a serious error (fatal error) will occur, and an error message "Uncaught exception" (uncaught exception) is output.
1. Processing procedures should include:

1.) Try-the function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.
2.) Throw-this specifies how the exception is triggered. Each "throw" must correspond to at least one "catch"
3.) Catch-the "catch" code block catches the exception and creates an object containing the exception information

2. Re-throwing exceptions

Sometimes, when an exception is thrown, you might want to handle it in a different way from the standard. You can throw an exception again in a "catch" code block.
The script should hide the system error from the user. For programmers, system errors may be important, but users are not interested in them. To make it easier for users to use, you can again throw exceptions with a friendly message to the user.

3. Rules for exceptions

1). Code that requires exception handling should be placed inside a try code block to catch a potential exception.
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 kinds of exceptions.
4). You can throw (re-thrown) exceptions again in a catch code block within a try code block.

In short: If an exception is thrown, it must be captured.

http://www.bkjia.com/PHPjc/736790.html www.bkjia.com true http://www.bkjia.com/PHPjc/736790.html techarticle with logging, you can send information directly to other log servers, or to a designated e-mail address (or send via a mail gateway), or to an operating system ...

  • 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.