PHP error and Exception Handling Module example, Exception Handling Module
This example describes the PHP error and Exception Handling modules. We will share this with you for your reference. The details are as follows:
I. error types and basic debugging methods
PHP program errors generally occur in the following three fields:
Syntax error:
Syntax errors are the most common and easy to fix. For example, a semicolon is missing in the code. Such errors will prevent script execution.
Runtime error:
This type of error generally does not stop PHP script execution, but does prevent the current task. An error is output, but the php script continues to be executed.
Logical error:
This type of error is the most troublesome. It neither prevents script execution nor outputs error messages.
An exception occurs when a program is executed, or an event that interrupts normal commands and jumps to other program modules for further execution.
PHP Error Reporting level
E_ALL // all information value: 6143
E_ERROR // fatal runtime error value: 1
E_RECOVERABLE_ERROR // near fatal runtime error. If it is not captured, it is regarded as E_ERROR value: 4096
E_WARNING // runtime warning (non-fatal error) value: 2
E_PARSE // parsing error value at Compilation: 4
E_NOTICE // runtime reminder (often a bug, or intentionally) value: 8
E_STRICT // code standardization warning (how to modify to forward compatibility) value: 2048
E_CORE_ERROR // critical error value during PHP initialization: 16
E_CORE_WARNING // warning (non-fatal error) value during PHP initialization at startup: 32
E_COMPILE_ERROR // fatal error value during compilation: 64
E_COMPILE_WARNING // The value of warning (non-fatal error) during compilation: 128
E_USER_ERROR // custom fatal error value: 256
E_USER_WARNING // User-Defined warning (non-fatal error) value: 512
E_USER_NOTICE // User-Defined reminder (often bug) value: 1024
Php. ini configuration file
Display_errors: whether to enable PHP to output error reports
Values: On (default Output Error Report) and Off (all error messages are blocked)
In the PHP script, you can call the ini_set () function to dynamically set the php. ini configuration file.
For example: ini_set ("display_errors", "On"); // display all error messages
Error_reporting: set different error reporting levels.
Error_reporting = E_ALL &~ E_NOTICE
-- Can throw any non-Attention error, default value
Error_reporting = E_ERROR | E_PARSE | E_CORE_ERROR
-- Only fatal runtime errors, new parsing errors, and core errors are considered.
Error_reporting = E_ALL &~ (E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE)
-- Reports all errors except user errors.
In the PHP script, you can dynamically set the error report level through the error_reporting () function. For example, error_reporting (E_ALL);]
Set error level instance: error. php
<H2> Test Error Report
PHP Error Reporting Behavior configuration instructions
Display_startup_errors = Off
Whether to display PHP engine initialization errors.
Log_errors = On
Determines the location of log statement records.
Error_log (null by default)
Specify the file or log of the error written into the system log syslog.
Log_errors_max_len = 1024
The maximum length of each log entry, in bytes. 0 indicates the maximum value.
Ii. error logs
You can record error logs in either of the following ways:
Use the specified file to record Error Report logs
The error log is recorded in the operating system log.
Use the specified file to record Error Report logs
1. Configure php. ini first:
Error_reporting = E_ALL // every error will be sent to PHP
Display_errors = Off // No error report is displayed
Log_errors = On // determines the location of log statement records.
Log_errors_max_log = 1024 // Maximum length of each log entry
Error_log = G:/myerror. log // specify the file to which the error is written.
2. Use a function: Use error_log () in the PHP file to record logs, and you can write the information to the myerror. log file.
For example:
Error_log ("Logon Failed! ");
3. Use four functions to record logs:
Define_syslog_variables (); // configure openlog () for System Log initialization; // open a log link syslog (); // send a log
Example
<? Phpif (! Ora_Logon ($ username, $ password) {error_log ("Oracle Database unavailable! ", 0); // write the error message to the operating system log.} if (! ($ Foo = allocate_new_foo () {error_log ("a big problem! ", 1," webmaster@www.mydomain.com "); // send to administrator mailbox} error_log (" screwed up! ", 2," localhost: 5000 "); // error_log (" screwed up! ", 3,"/usr/local/errors. log "); // send it to the specified file?>
<? Phpdefine_syslog_variables (); openlog ("PHP5", LOG_PID, LOG_USER); syslog (LOG_WARNING, "demo of a warning report sent to syslog, warning time :". date ("Y/m/dH: I: s"); closelog ();?>
View logs, for example, in windows, right-click my computer and choose manage> System Tools> Event Viewer> application options to view the log.
Iii. Exception Handling
Exception Processing is used to change the normal process of the script when a specified error occurs. It is a new important feature in PHP5. Exception Handling is a scalable and easy-to-maintain unified error handling mechanism, and provides a new object-oriented error handling method.
Exception Handling Format:
Try {// use try to include code that may cause exceptions. // if an exception occurs, try to catch the exception and submit it to catch for processing. // Throw an exception statement: throw an exception object .} Catch (exception object parameter) {// handle the exception here .} [Catch (.,,) {...}]