[PHP] Try-catch Exceptiong Handler

Source: Internet
Author: User

Http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning
One possibility is to set your own error handler before the call and restore the previous error handler later withRestore_error_handler().Set_error_handler(function() {/*Ignore Errors*/});d Ns_get_record ();Restore_error_handler(); You could build in this idea and write a re-usable error handler that logs the errors forYou.Set_error_handler([$logger, ' Onsilencederror ']);d Ns_get_record ();Restore_error_handler(); Turning errors into exceptionsyou can Use Set_error_handler() and the ErrorexceptionclassTo turn all PHP errors into exceptions.Set_error_handler(function($errno,$errstr,$errfile,$errline,Array $errcontext) { //error is suppressed with the @-operator if(0 = = =error_reporting()) { return false; } Throw NewErrorexception ($errstr, 0,$errno,$errfile,$errline);});Try{Dns_get_record ();}Catch(errorexception$e) { // ...}

The important thing to note when using your own error handler are that it'll bypass theerror_reportingSetting and pass all errors (notices, warnings, etc.) to your error handler. Can set a second argument onSet_error_handler() toDefineWhich error types you want to receive, or access the CurrentSetting using ... =error_reporting() inside the error handler.

[PHP] Try-catch Exceptiong Handler

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.