我能试着/抓住警告吗?我需要捕捉从某些php本机函数抛出的一些警告,然后处理它们。具体而言:array dns_get_record ( string $hostname [, int $type= DNS_ANY [, array &$authns [, array &$addtl ]]] )当DNS查询失败时,它将引发警告。try/catch不起作用,因为警告也不例外。我现在有两个选择:set_error_handler似乎有点过分,因为我必须使用它来过滤页面中的每一个警告(这是真的吗?)调整错误报告/显示,使这些警告不会被回显到屏幕上,然后检查返回值;如果它是false,没有找到主机名的记录。这里的最佳做法是什么?
3 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
设置和恢复错误处理程序
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
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) { // ...}
error_reporting
set_error_handler()
... = error_reporting()
抑制警告
dns_get_record()
千万里不及你
TA贡献1784条经验 获得超9个赞
E_WARNING
set_error_handler("warning_handler", E_WARNING);dns_get_record(...)restore_error_handler();function warning_handler($errno, $errstr) { // do something}
吃鸡游戏
TA贡献1829条经验 获得超7个赞
@
@mysql_query( '...' )
bob@mypc:~$ php -aInteractive shell php > echo @something(); // this will just silently die...
bob@mypc:~$ php -aInteractive shell php > echo something(); // lets try it again but don't suppress the errorPHP Fatal error: Call to undefined function something() in php shell code on line 1PHP Stack trace:PHP 1. {main}() php shell code:0bob@mypc:~$
- 3 回答
- 0 关注
- 263 浏览
添加回答
举报
0/150
提交
取消