PHP exception handling, error reports, logs

Source: Internet
Author: User
Tags php exception handling
In php, we often encounter some errors to be handled. next I will summarize the exception handling, error reports, and logs in php. Error handling: 1 Syntax error 2 Runtime

In php, we often encounter some errors to be handled. next I will summarize the exception handling, error reports, and logs in php.

Error handling:

1. Syntax error

2. runtime error

3. logical error

Error report:

Error: The E_ERROT program is interrupted and an error occurs.

Warning the E_WARNING program will not be interrupted, but some functions may not be implemented.

Note: E_NOTICE does not affect the program and can be completely blocked.

Output all error reports during development and disable all error reports during runtime

Write errors to logs:

1. enable the log (error_log = On in php. ini) and disable the error report. The log will be recorded if an error occurs but cannot be output directly.

2. if no log path is specified, logs are written to the web server by default.

Set error reports:

Error_reporting (E_ALL) // output all reports

Modify the php. ini configuration file with the following code:

Ini_set ("display_errors", off) // modify it to not display error reports

Ini_get ("upload_max_filesize") // read the size limit of uploaded files in the configuration file

Exception handling:

It is an unexpected event that occurs during the running of the program. it uses exceptions to change the normal process of the script. PHP 5 provides a new object-oriented error handling method, exception handling is used to change the normal process of the script when a specified error (exception) occurs, which is called an exception.

When an exception is triggered, it usually occurs:

The current code status is saved, and code execution is switched to the predefined exception processor function. depending on the situation, the processor may re-execute the code from the saved code status and terminate the script execution, or execute the script from another position in the code.

We will display different error handling methods:

The basic usage of exceptions: create a custom exception processor, add multiple exceptions, throw an exception again, and set the top-level exception processor. the syntax code is as follows:

  1. Try {
  2. Code that may be wrong
  3. Throw new Exception ("Exception information ")
  4. } Catch (Exception $ e [Exception object]) {
  5. Subsequent normal code
  6. }

The instance code is as follows:

  1. Function runtimeErrorHandler ($ level, $ string)
  2.  
  3. {
  4. // Manually throw an exception instance during custom error handling
  5. // In order to display the error code as well, the error code and the error message are spliced here as new error messages for transmission.
  6. Throw new Exception ($ level. '|'. $ string );
  7. }
  8. // Set the custom error handling function
  9. Set_error_handler ("runtimeErrorHandler ");
  10. Try
  11. {
  12. $ A = 2/0;
  13. // Create a zero division error that cannot be intercepted previously
  14. }
  15. Catch (Exception $ e)
  16. {
  17. Echo 'error message: ', $ e-> getMessage ();
  18. // An error is displayed. here we can see the error level and error message "2 | Division by zero"
  19. }

1. if the code in try is not abnormal, it will be executed normally.

2. if there is an exception in the try code, an exception object will be thrown. capture $ e in catch () and point to the exception object, and then continue to execute.

3. $ e-> getMessage () to get exception information

Custom exception class:

Purpose: write some methods to solve specific exceptions (the built-in classes do not have a processing method)

1. the custom Exception class must be a subclass of the Exception (built-in class ).

2. in the Exception class, only the constructor and toString () can be rewritten.

3. define the required methods

Abnormal rules

Code that requires exception handling should be placed in the try code block to capture potential exceptions. each try or throw code block must have at least one catch code block, multiple catch code blocks can capture different types of exceptions. you can throw (re-thrown) exceptions again in the catch code block in the try code block.

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.