Questions about the Set_error_handle and at (@) symbols
Source: Internet
Author: User
Set_error_handle and at (@) sign Issues
The problem is, I use Set_error_handle to write some system errors or warning messages to the log file.
However, for some APIs such as fopen, if the file does not exist in the case will also run to their own error-handling function, before the fopen with the @ symbol before the display of such problems.
If I want to use set_error_handle, I can not remove the @ function. How do you do that?
Thank you, heroes.
------Solution--------------------
Capturing with Try Catch
------Solution--------------------
can be removed, using Set_error_handler you only need to pay attention to two points:
1.set_error_handler only captures the type of error defined in the error_reporting, which can be configured in php.ini, or it can be set using functions;
2. The return value of the custom error-handling function, if FALSE, triggers the default error handling for PHP. A return value of true does not trigger PHP's default error handling, and you won't see that annoying bug.
As for the wording, you can refer to it.
function MyErrorHandler ($errno, $errstr, $errfile, $errline)
{
if (! ( Error_reporting () & $errno)) {
If this error type is not included in the error_reporting
Return
}
Switch ($errno) {
Case E_USER_ERROR:
echo "
My ERROR[$errno] $errstr
\ n ";
echo "Fatal error on line $errline in file $errfile";
echo ", PHP". Php_version. " (" . Php_os. ")
\ n ";
echo "Aborting ...
\ n ";
Exit (1);
Break
Case e_user_warning:
echo "
My WARNING[$errno] $errstr
\ n ";
Break
Case E_user_notice:
echo "
My NOTICE[$errno] $errstr
\ n ";
Break
Default
echo "Unknown error type: [$errno] $errstr
\ n ";
Break
}
Returning false will trigger the default error handling of PHP, and back to true is handled only with custom functions.
return true;
}
Set_error_handler (' MyErrorHandler ');
fopen ('/tmp/foo ', ' R ');
------Solution--------------------
Tested, #5的代码可以满足你的要求!
After execution, if
fopen ('/tmp/foo ', ' R ');
Fails, the display
Unknown error Type: [2] fopen (/tmp/foo) [function.fopen]: failed to open stream:no such file or directory
If
@fopen ('/tmp/foo ', ' R ');
Fails, then nothing appears
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service