In php development, we usually use the built-in error handling method of php to handle some errors, but some of us need to customize some error processing mechanisms to solve the problems that cannot be solved by the system.
Basic Error Handling: Use the die () function
The first example shows a simple script for opening a text file:
The Code is as follows: |
Copy code |
<? Php $ File = fopen ("welcome.txt", "r "); ?> |
If the file does not exist, you will get an error similar to this:
Warning: fopen(welcome.txt) [function. fopen]: failed to open stream:
No such file or directory in C: webfoldertest. php on line 2 to Prevent Users From getting error messages similar to the above, we can check whether the file exists before accessing the file:
The Code is as follows: |
Copy code |
<? Php If (! File_exists ("welcome.txt ")) { Die ("File not found "); } Else { $ File = fopen ("welcome.txt", "r "); } ?> |
Now, if the file does not exist, you will get an error message similar to this:
File not found is more effective than the previous Code because it uses a simple error handling mechanism to terminate the script after the error.
However, simply terminating the script is not always the proper method. Let's look at the alternative PHP functions used to handle errors.
Next, let's look at a custom error handling function.
The Code is as follows: |
Copy code |
Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline ){ If (! (Error_reporting () & $ errno) {return ;} Switch ($ errno ){ Case E_USER_ERROR: Echo "<B> My ERROR </B> [$ errno] $ errstr <br/> "; Echo "error line: $ errline in file: $ errfile <br/> "; Echo "PHP version:". PHP_VERSION. "(". PHP_ OS. ") <br/> "; Break; Case E_USER_WARNING: Echo "<B> My WARNING </B> [$ errno] $ errstr <br/> "; Break; Case E_USER_NOTICE: Echo "<B> My NOTICE </B> [$ errno] $ errstr <br/> "; Break; Default: Echo "Unknown error type: [$ errno] $ errstr <br/> "; Break; } Return true; } Function trigger_test ($ age) {// throw an error in the test function If ($ age <= 0 | $ age> 999) trigger_error ("invalid age: $ age", E_USER_ERROR ); If ($ age <18) trigger_error ("minor: $ age", E_USER_WARNING ); If ($ age> 40 & $ age <100) trigger_error ("slightly older: $ age", E_USER_NOTICE ); } // If the error is handled in a simple and unified manner: $ ErrorHandler = set_error_handler ("myErrorHandler "); Trigger_test (1000); // an error-level error is thrown. Function myError ($ errno, $ errstr, $ errfile, $ errline ){ Print_r (func_get_args ()); // Specific Handling Method } Function myWarning ($ errno, $ errstr, $ errfile, $ errline ){ Print_r (func_get_args ()); // Specific Handling Method }
Function myNtice ($ errno, $ errstr, $ errfile, $ errline ){ Print_r (func_get_args ()); // Specific Handling Method } |
// If you want to handle different error levels separately:
The Code is as follows: |
Copy code |
Set_error_handler ('myerror', E_USER_ERROR ); Set_exception_handler ('mywarning', E_USER_WARNING ); Set_exception_handler ('myntice ', E_USER_NOTICE );
|
Trigger_error ('intentionally throw an error, which one is very serious! ', E_USER_ERROR );
The following are some php error code details.
Parameters |
Description |
Error_level |
Required. Specifies the error report level for user-defined errors. It must be a value. See the following table: Error Report level. |
Error_message |
Required. Specifies an error message for a user-defined error. |
Error_file |
Optional. Specifies the file name in which the error occurs. |
Error_line |
Optional. Specifies the row number of the error. |
Error_context |
Optional. Define an array that contains each variable and their values when an error occurs. |
Error Report Level
These error reporting levels are different types of errors that the error handler aims to handle:
Value |
Constant |
Description |
2 |
E_WARNING |
Non-fatal run-time error. Do not pause script execution. |
8 |
E_NOTICE |
Run-time notification. An error may occur when the script runs normally. |
256 |
E_USER_ERROR |
Fatal user-generated error. This is similar to the E_ERROR set by the programmer using the PHP function trigger_error. |
512 |
E_USER_WARNING |
Non-fatal user-generated warning. This is similar to the E_WARNING set by the programmer using the PHP function trigger_error. |
1024 |
E_USER_NOTICE |
User-generated notifications. This is similar to the E_NOTICE set by the programmer using the PHP function trigger_error. |
4096 |
E_RECOVERABLE_ERROR |
Possible fatal errors. Similar to E_ERROR, but can be captured by a user-defined handler. (See set_error_handler ()) |
8191 |
E_ALL |
All errors and warnings except level E_STRICT. (In PHP 6.0, E_STRICT is part of E_ALL) |