PHP error handling and exception handling notes _php Tutorial

Source: Internet
Author: User
Tags php error
Summarize PHP's error handling to the newcomer.

PHP provides error handling and logging capabilities. These functions allow you to define your own error handling rules and how to modify the error records. In this way, you can change and strengthen the error output information to meet the actual needs according to your own needs.

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?

• User-friendly When the site is in error
• Better avoiding errors, debugging, fixing errors
• Avoid some security risks
• Better assurance of program robustness
•......
1. 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

mysql_connect (' localhost ', www.bKjia.c0m, ' 123456 ')
Or Die (' Connection Database error: '. mysql_error ());

However, simply terminating the script is not always the proper way.

2. 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.
Defining 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.

Pre-determined variables

value Constants Description Notes
1 < span="">(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 < span="">(integer) Run-time warning (non-fatal error). Only the prompt is given, but the script does not terminate the run.
4 < span="">(integer) Compile-time syntax parsing error. Parsing errors are generated only by the parser.
8 < span="">(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 < span="">(integer) A fatal error occurred during PHP initialization startup. This error is < span=""> similar, but is generated by the PHP engine core. Since PHP 4
32 < span="">(integer) PHP initialization warnings (non-fatal errors) that occur during startup. Similar < span=""> , but is generated by the core of the PHP engine. Since PHP 4
64 < span="">(integer) Fatal compile-time error. Similar < span=""> , but is generated by the Zend scripting engine. Since PHP 4
128 < span="">(integer) Compile-time warning (non-fatal error). Similar < span=""> , but is generated by the Zend scripting engine. Since PHP 4
256 < span="">(integer) User-generated error message. Similar < span=""> , but is generated by the user's own use of PHP function Trigger_error () in the code. Since PHP 4
512 < span="">(integer) User-generated warning message. Similar < span=""> , but is generated by the user's own use of PHP function Trigger_error () in the code. Since PHP 4
1024 < span="">(integer) User-generated notification information. Similar < span=""> , but is generated by the user's own use of PHP function Trigger_error () in the code. Since PHP 4
2048 < span="">(integer) Enable PHP recommendations for code modifications to ensure the best interoperability and forward compatibility of your code. Since PHP 5
4096 < span="">(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-defined handle (see Set_error_handler ()), it will < span=""> become one so that the script will terminate running. Since PHP 5.2.0
8192 < span="">(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 < span="">(integer) The user produces less warning messages. Similar < span=""> , but is generated by the user's own use of PHP function Trigger_error () in the code. Since PHP 5.3.0
30719 < span="">(integer) < span="">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.

Trigger Error
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.

Possible types of errors:
e_user_error– A fatal user-generated run-time error. The error cannot be recovered. The execution of the script was interrupted.
e_user_warning– a non-fatal user-generated run-time warning. Script execution is not interrupted.
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:

1
2
Trigger_error ("Wrong ah", e_user_warning);
Output Warning: Error message in XXXX
3. 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

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

The processing handler should include:

1.try– functions that use exceptions 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– here specifies how to trigger an exception. Each "throw" must correspond to at least one "catch"
3.catch– catch blocks catch exceptions and create an object that contains exception information
To re-throw an exception

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.

Rules for exceptions

The code that requires exception handling should be placed inside a try code block to catch a potential exception.
Each try or throw code block must have at least one corresponding catch code block.
You can use multiple catch blocks to catch different kinds of exceptions.
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/699429.html www.bkjia.com true http://www.bkjia.com/PHPjc/699429.html techarticle summarize PHP's error handling to the newcomer. PHP provides error handling and logging capabilities. These functions allow you to define your own error handling rules and how to modify the Error records ...

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