In PHP development, we generally use PHP's own error handling methods to handle some errors, but some of us need to customize some of the error handling mechanism to solve the problem that the system can not solve.
Basic error handling: Using the Die () function
The first example shows a simple script that opens a text file:
The code is as follows |
Copy Code |
$file =fopen ("Welcome.txt", "R"); ?> |
If the file does not exist, you will get an error like 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 a similar error message, we detect if the file exists before accessing the file:
The code is as follows |
Copy Code |
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 like this:
File not found is more efficient than the previous code because it takes a simple error handling mechanism to terminate the script after the error.
However, simply terminating the script is not always the proper way. Let's look at the alternative PHP functions for handling errors.
Let's take a 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 "My ERROR [$errno] $errstr "; echo "Error line: $errline in file: $errfile "; echo "PHP version:". Php_version. " (" . Php_os. ") "; Break Case e_user_warning: echo "My WARNING [$errno] $errstr "; Break Case E_user_notice: echo "My NOTICE [$errno] $errstr "; Break Default echo "Unknown error type: [$errno] $errstr "; Break } return true; } function Trigger_test ($age) {//The test function that throws the error if ($age <= 0 | | $age > 999) trigger_error ("Age not valid: $age years old", E_user_error); if ($age < Trigger_error) ("Minors: $age Years", e_user_warning); if ($age > && $age < Trigger_error) ("Slightly older: $age years old", E_user_notice); } If the error is handled simply and uniformly: $errorHandler = Set_error_handler ("MyErrorHandler"); Trigger_test (1000);//throws an error level function Myerror ($errno, $errstr, $errfile, $errline) { Print_r (Func_get_args ()); Specific Treatment methods } function mywarning ($errno, $errstr, $errfile, $errline) { Print_r (Func_get_args ()); Specific Treatment methods }
function Myntice ($errno, $errstr, $errfile, $errline) { Print_r (Func_get_args ()); Specific Treatment methods } |
If you want to handle different levels of errors individually:
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 throwing a mistake, or a very serious one! ', e_user_error);
The following is also attached to PHP some error code detailed
Parameters |
Description |
Error_level |
Necessary. Specifies the error reporting level for user-defined errors. Must be a number of values. See the table below: Error reporting levels. |
Error_message |
Necessary. Specifies error messages for user-defined errors. |
Error_file |
Optional. Specifies the file name in which the error occurred. |
Error_line |
Optional. Specifies the line number where the error occurred. |
Error_context |
Optional. Specifies an array that contains each variable that was used when the error occurred and their value. |
Error Reporting level
These error reporting levels are different types of errors that the error handler is designed to handle:
value |
Constants |
Description |
2 |
E_warning |
A non-fatal run-time error. Script execution is not paused. |
8 |
E_notice |
Run-time notice. The script discovers that an error may have occurred, but it may also occur when the script is running correctly. |
256 |
E_user_error |
A fatal user-generated error. This is similar to the e_error that programmers use to set the PHP function Trigger_error (). |
512 |
E_user_warning |
A non-fatal user-generated warning. This is similar to the e_warning that programmers use to set the PHP function Trigger_error (). |
1024 |
E_user_notice |
User-generated notifications. This is similar to the e_notice that programmers use to set the PHP function Trigger_error (). |
4096 |
E_recoverable_error |
A fatal error that can be caught. Similar to E_error, but can be captured by user-defined handlers. (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) |
http://www.bkjia.com/PHPjc/629638.html www.bkjia.com true http://www.bkjia.com/PHPjc/629638.html techarticle in PHP development we generally use PHP's own error handling methods to handle some errors, but some we need to customize some error handling mechanism to solve the system can not be solved ...