In PHP we use the most common error mechanism has try catch{} this to handle can be very instant noodles catch to error, but in PHP for a lot of also provide error view and error closed this can be processed in php.ini can also be in the file at the beginning of the Error_display (0 ), to not show errors
| The code is as follows |
Copy Code |
|
$a = fopen (' test.txt ', ' R ');
There is no judgment on the file opened, if the file does not exist will be an error
?> |
Then the correct wording should be as follows:
| The code is as follows |
Copy Code |
|
if (file_exists (' Test.txt ')) {
$f =fopen (' Test.txt ', ' R ');
Close when you are finished using
Fclose ($f);
}
?> |
One, PHP error handling of three ways A, simple die () statement;
Equivalent to exit ();
Cases:
| The code is as follows |
Copy Code |
|
if (!file_exists (' Aa.txt ')) {
Die (' file does not exist ');
} else {
Perform actions
}
If the above die () is triggered, then ECHO is not executed here
echo ' OK '; |
Concise wording:
| The code is as follows |
Copy Code |
|
File_exits (' aaa.txt ') or Die (' File not present ');
echo ' OK '; |
B, custom errors and error triggers
1, error processor (custom error, commonly used for syntax error handling)
Create a custom error function (processor) that must be capable of handling at least two parameters (Error_level and errormessage), but can accept up to five parameters (Error_file, Error_line, Error_context)
Grammar:
function Error_function ($error _level, $error _message, $error _file, $error _line, $error _context)
You will also need to overwrite Set_error_handler () when you create it;
Set_error_handler (' error_function ', e_warning);//Here error_function corresponds to the custom processor name created above, and the second parameter is the error level using the custom error handler;
Error Reporting level (learn it)
These error reporting levels are different types of errors that the error handler is designed to handle:
Value constant Description
2 e_warning 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.
The e_user_error fatal user-generated error. This is similar to the e_error that programmers use to set the PHP function Trigger_error ().
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 ().
1024x768 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 captured. 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)
2. Error trigger (typically used to handle logical errors)
Requirements: For example to receive an age, if the number is greater than 120, it is considered a mistake
Traditional methods:
if ($age >120) {
Echo ' age error '; exit ();
}
Using triggers:
if ($age >120) {
Trigger_error (' Error message ' [, ' Error level ']), where the error level is optional and is used to define the level of the error
User-defined levels include the following three types: E_user_warning, E_user_error, E_user_notice
Trigger_error (' age error ');//This is the default error handling of the system being called, we can also use the custom processor
}
Custom processor, same as above
function Myerror ($error _level, $error _message) {
Echo ' Error text ';
}
Also need to change the system default processing function
Set_error_handler (' Myerror ', e_user_warning);//with above, the first parameter is the name of the custom function, and the second is the error level "Here is usually the following three kinds of error levels: E_user_warning, E_user _error, E_user_notice "
You can now use the custom error-handling function with Trigger_error.
Exercises:
| The code is as follows |
Copy Code |
|
Date_default_timezone_set (' PRC ');
function Myerror ($error _level, $error _message) {
$info = "Error number: $error _leveln";
$info. = "error message: $error _messagen";
$info. = ' Occurrence time: '. Date (' y-m-d h:i:s ');
$filename = ' aa.txt ';
if (! $fp =fopen ($filename, ' a ')) {
' Create file '. $filename. ' Failure ';
}
if (is_writeable ($filename)) {
if (!fwrite ($FP, $info)) {
Echo ' Write file failed ';
} else {
Echo ' has successfully logged error messages ';
}
Fclose ($FP);
} else {
echo ' file '. $filename. ' Not writable ';
}
Exit ();
}
Set_error_handler (' Myerror ', e_warning);
$fp =fopen (' Aaa.txt ', ' R ');
?> |
1 2
http://www.bkjia.com/PHPjc/632215.html www.bkjia.com true http://www.bkjia.com/PHPjc/632215.html techarticle in PHP We use the most common error mechanism has try catch{} this to handle can be very instant noodles catch to error, but in PHP for a lot of also provide error view and error close this can ...