PHP exception handling, error reports, and logs. 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 errors in php we often encounter some errors to be handled. next I will summarize the exception handling, error reports, and log content 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:
Ini_set ("display_errors", off) // modify it to the ini_get ("upload_max_filesize") // read the size limit of uploaded files in the configuration file
An unexpected event occurs during the running of the program. use exceptions to change the normal process of the script and handle exceptions:
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. This is called an exception.
When an exception is triggered, it usually occurs:
• The current code status is saved
• Code execution is switched to a predefined exception processor function
• Depending on the situation, the processor may re-execute the code from the saved code status, terminate the script execution, or continue to execute the script from another location in the code
We will display different error handling methods:
• Basic usage of exceptions
• Create a custom exception processor
• Multiple exceptions
• Throw an exception again
• Set the top-layer exception processor
Syntax:
Try {code that may be faulty throw new Exception ("Exception information")} catch (Exception $ e [Exception object]) {subsequent normal code}
Example
Function runtimeErrorHandler ($ level, $ string) {// when handling custom errors, an exception instance is manually thrown. // to display the error code, the error code and the error message are spliced here for transmission as the new error message. Throw new Exception ($ level. '| '. $ string) ;}// set the custom error handling function set_error_handler ("runtimeErrorHandler"); try {$ a = 2/0; // create a zero-except error that cannot be intercepted previously} catch (Exception $ e) {echo 'error message: ', $ e-> getMessage (); // Display error, here we can see the error level and error message "2 | Division by zero "}
2. if there is an exception in the try code, an exception object is thrown and $ e is captured in catch () to point to the exception object. Continue to run down. 1. if there is no exception in the try code, it will be executed normally.
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.
• A re-thrown exception can be thrown again in the catch code block in the try code block.
The above are PHP exception handling, error reports, and log details. For more information, see PHP Chinese website (www.php1.cn )!
Bytes. Error handling: 1. Syntax error...