PHP exception handling, error reporting, log detailed

Source: Internet
Author: User
Tags php exception handling
In PHP we will often encounter a number of errors to deal with, I would like to summarize in PHP exception handling, error reporting, some summary of the log content and introduction.

Error handling:

1. Syntax errors

2. Run-time error

3. Logic Error

Error Reporting:

Error: E_errot program is interrupted, error occurred

Warning: The e_warning program will not be interrupted, but some features may not be implemented

Note: E_notice does not affect the program, it can completely block

Output all error reports at development time, disable all error reports at run time

To write an error to the log:

1. Turn on log (Error_log = on in php.ini), and turn off error reporting, error (if it happens but no direct output allowed) log will be logged

2. If you do not specify a log path, it will be written to the Web server log by default

To set up error reporting:

Error_reporting (e_all)//Output all reports

To modify the php.ini configuration file:

Ini_set ("Display_errors", off)//modified to not show error report Ini_get ("upload_max_filesize")//Read config file upload file size limit

Accidental, is the unexpected thing that happens in the program running, use exception to change script normal process exception handling:

PHP 5 provides a new approach to object-oriented error handling.

Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs. This condition is called an exception.

When an exception is triggered, it usually occurs:

• current code state is saved
• code execution is switched to pre-defined exception handler function
• Depending on the situation, the processor may start executing the code again from the saved code state, terminating the script execution, or continuing the script from another location in the code
We will show you different error handling methods:

• Basic use of exceptions
• Create a custom exception handler
• Multiple exceptions
• re-throw exceptions
• set top-level exception handlers

Grammar:

try{possible error code throw new Exception ("Exception Information")}catch (Exception $e [Exception object]) {Normal code after}

Cases

function   Runtimeerrorhandler ($level, $string) {//Custom error handling, manually throw an exception instance//in order to display the error level code, The error code and error messages are stitched together as new error messages. Throw   new   Exception ($level. ' | '. $string); }//Set custom error handling function Set_error_handler ("Runtimeerrorhandler"); try{$a =2/0;  This creates a previously impossible to intercept except 0 error}catch (Exception $e) {echo ' Error message: ', $e->getmessage ();//Display error, here you can see the error level and error message "2| Division by Zero "}

2. If the code in the try has an exception, an exception object is thrown, and the catch () $e points to the exception object. Continue execution down 1. If the code in the try does not have an exception, it executes normally.

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

Custom Exception classes:

Role: Write some methods to resolve specific exceptions (built-in classes have no processing methods)

1. Custom exception class, must be a subclass of exception (built-in class)

Only the constructor method and ToString () in the 2.Exception class can be overridden

3. Define the required methods

Rules for exceptions

• code that requires exception handling should be placed inside a try code block to catch potential exceptions.
• each try or throw code block must have at least one corresponding catch code block.
• use multiple catch blocks to catch different kinds of exceptions.
• can throw (re-thrown) exceptions again in a catch code block within a try code block.

The above is the PHP exception handling, error reporting, the contents of the log detailed, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

http://www.bkjia.com/PHPjc/633087.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633087. Htmltecharticle in PHP We will often encounter a number of errors to deal with, I would like to summarize in PHP exception handling, error reporting, some of the contents of the log summary and introduction. Error handling: 1. Syntax error ...

  • Related Article

    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.