本文介绍了我可以尝试/抓住警告吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具体来说:

  array dns_get_record(string $ hostname [,int $ type = DNS_ANY [,array& $ authns [,array& $ addtl]]] 

当DNS查询失败时,会引发警告。



尝试 / catch 不起作用,因为警告不是例外。



我现在有两个选项:


  1. set_error_handler 因为我必须使用它来过滤页面中的每个警告(这是真的吗?);


  2. 调整错误报告/显示,以便这些警告不会得到回显屏幕,然后检查返回值;如果它是 false ,则没有找到主机名的记录。


这里最好的做法是什么?

解决方案

设置并恢复错误处理程序



一种可能性是在调用之前设置自己的错误处理程序,然后使用 restore_error_handler()恢复以前的错误处理程序。

  set_error_handler(function(){/ * ignore errors * /}); 
dns_get_record();
restore_error_handler();

您可以基于这个想法,并编写一个可重用的错误处理程序,为您记录错误。

  set_error_handler([$ logger,'onSilencedError']); 
dns_get_record();
restore_error_handler();

将错误转化为例外



您可以使用 set_error_handler() ErrorException 类将所有php错误转换为异常。 / p>

  set_error_handler(function($ errno,$ errstr,$ errfile,$ errline,array $ errcontext){
//如果(0 === error_reporting()){
return false;
}

抛出新的ErrorException($ errstr, 0,$ errno,$ errfile,$ errline);
});

try {
dns_get_record();
} catch(ErrorException $ e){
// ...
}

使用自己的错误处理程序时要注意的重要事情是它将绕过 error_reporting 设置并将所有错误(通知,警告等)传递给你的错误处理程序您可以在 set_error_handler()上设置第二个参数,以定义要接收的错误类型,或使用 ... = error_reporting访问当前设置()



禁止警告



另一种可能性是用@操作符来抑制呼叫,然后检查 dns_get_record()的返回值。 但是我建议反对这个,因为错误/警告被触发以被处理,不被压制。


I need to catch some warnings being thrown from some php native functions and then handle them.

Specifically:

array dns_get_record  ( string $hostname  [, int $type= DNS_ANY  [, array &$authns  [, array &$addtl  ]]] )

It throws a warning when the DNS query fails.

try/catch doesn't work because a warning is not an exception.

I now have 2 options:

  1. set_error_handler seems like overkill because I have to use it to filter every warning in the page (is this true?);

  2. Adjust error reporting/display so these warnings don't get echoed to screen, then check the return value; if it's false, no records is found for hostname.

What's the best practice here?

解决方案

Set and restore error handler

One possibility is to set your own error handler before the call and restore the previous error handler later with restore_error_handler().

set_error_handler(function() { /* ignore errors */ });
dns_get_record();
restore_error_handler();

You could build on this idea and write a re-usable error handler that logs the errors for you.

set_error_handler([$logger, 'onSilencedError']);
dns_get_record();
restore_error_handler();

Turning errors into exceptions

You can use set_error_handler() and the ErrorException class to turn all php errors into exceptions.

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
    // error was suppressed with the @-operator
    if (0 === error_reporting()) {
        return false;
    }

    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

try {
    dns_get_record();
} catch (ErrorException $e) {
    // ...
}

The important thing to note when using your own error handler is that it will bypass the error_reporting setting and pass all errors (notices, warnings, etc.) to your error handler. You can set a second argument on set_error_handler() to define which error types you want to receive, or access the current setting using ... = error_reporting() inside the error handler.

Suppressing the warning

Another possibility is to suppress the call with the @ operator and check the return value of dns_get_record() afterwards. But I'd advise against this as errors/warnings are triggered to be handled, not to be suppressed.

这篇关于我可以尝试/抓住警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:28