Set_error_handle and at (@) symbols

Source: Internet
Author: User
Tags try catch
The problem with the set_error_handle and at (@) symbols is that I use set_error_handle to write some system errors or warning information to the log file. But for some APIs such as fopen, if the file does not exist, it will also go to the self-written error handler function, [email & #160; protected], do not display such issues. [Email & #160; question about the prot set_error_handle and at (@) symbols
The problem is that I use set_error_handle to write some system errors or warnings to the log file.
But for some APIs such as fopen, if the file does not exist, it will also go to the self-written error handler function, [email protected], and will not display such problems.
[Email protected] is removed. How can this problem be solved?
Thank you!


------ Solution --------------------
Capture with try catch
------ Solution --------------------
You can remove it. to use set_error_handler, you only need to pay attention to two points:
1. set_error_handler only captures the error type defined in error_reporting. this can be configured in php. ini or use function settings;
2. if the returned value of the custom error handling function is false, the default php error handling will be triggered. If the returned value is true, the default php error handling will not be triggered, and the annoying error cannot be seen.

For the writing method, refer.
Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline)
{
If (! (Error_reporting () & $ errno )){
// If this error type is not included in 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;
}
// If false is returned, the default php error processing is triggered. if true is returned, only custom functions are used for processing.
Return true;
}
Set_error_handler ('myerrorhandler ');
Fopen ('/tmp/Foo', 'r ');
------ Solution --------------------
Tested, #5 code can meet your requirements!
After execution, if
Fopen ('/tmp/Foo', 'r ');
Failed, displayed
Unknown error type: [2] fopen (/tmp/foo) [function. fopen]: failed to open stream: No such file or directory

If
@ Fopen ('/tmp/Foo', 'r ');
Failed, no display

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.