PHP 错误报告和 mail()

发布于 2024-11-17 19:36:30 字数 1136 浏览 3 评论 0原文

目前,每当遇到任何问题时,set_error_handler()都会抛出ErrorException。此外,我在 -1 处得到了 error_reporting(),因此错误索引数组会引发异常。

无论如何,这是我的错误/报告环境的粗略概述;在用于项目的过程中,PHP mail() 函数在我的 (Win7) 开发计算机上抛出错误,因为我没有安装邮件服务器。我尝试在它前面添加 @ 前缀以将其关闭,但它仍然会触发错误,从而引发异常。我可以用 try{} 包装它,但我很好奇为什么它不会关闭。

为什么不闭嘴呢?

重申一下,我们几乎可以从等式中删除mail()据我所知);我只是想知道为什么 @ 在这种情况下不能使用 mail() 或任何函数来完成它的工作。我只是认为 mail() 由于某种原因可能是特殊情况。


感谢我们的朋友 XDebug

( ! ) ErrorException: mail() [function.mail]: 无法连接到“localhost”端口 25 的邮件服务器,请验证 php.ini 中的“SMTP”和“smtp_port”设置或使用 C 中的 ini_set() :\xampp\htdocs\dbc_relaunch_2-0-0\mf\handlers\api\mail\send.php 第 16 行


好吧,我已经走了与:

try{
    mail($args);
catch(Exception $exception){
    return $failure;
}
return $success;

而不是:

if(@mail($args)){
    return $failure;
}
return $success;

一切都很好;我正在浏览 @Pheonix 链接的答案,但任何有简短回复的人都可以免费回答。

I've currently got set_error_handler() throwing ErrorException whenever anything is encountered. Furthermore, I've got error_reporting() at -1, so mis-indexing an array throws an exception.

Anyways, that being a cursory overview of my error/reporting environment; the PHP mail() function is tossing errors on my (Win7) dev machine, during a process intended for a project, 'cause I don't have a mail server installed. I tried prefixing it with @ to shut it up, but it still triggers an error, and hence throws an exception. I could wrap it with try{}, but I'm curious as to why it won't shut up.

Why won't it shut up?

To reiterate, we can almost remove mail() from the equation (so far as I can tell); I just want to know why @ wouldn't do it's job under such a circumstance, with mail() or any function. I just figured mail() may be special case for some reason.


Thanks to our friend XDebug:

( ! ) ErrorException: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\dbc_relaunch_2-0-0\mf\handlers\api\mail\send.php on line 16


Alrighty, I've simply gone with:

try{
    mail($args);
catch(Exception $exception){
    return $failure;
}
return $success;

Rather than:

if(@mail($args)){
    return $failure;
}
return $success;

And all is well; I'm browsing the answer @Pheonix linked, but anyone with a short order response as to why this is free to answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名。 2024-11-24 19:36:30

您能向我们展示您的错误处理程序吗?请注意,无论您的 error_reporting() 设置如何,即使您在函数前面添加 @,发生错误时都会调用您的处理程序。

特别值得注意的是这个值
如果导致的语句将为 0
该错误由 @ 前缀
错误控制运算符。

来自 set_error_handler() 的手册页。

如果您不想针对由 @ 前缀表达式引起的错误引发异常,则必须在处理程序中检查 error_reporting() 的返回值。如果它返回零,则您正在处理一个被抑制的错误。

Could you show us your error handler? Note that your handler will be called in case of an error, regardless of your error_reporting() setting and even if you prepend your function with an @.

Of particular note is that this value
will be 0 if the statement that caused
the error was prepended by the @
error-control operator.

From the manual page of set_error_handler().

If you don't want to throw exceptions for errors caused by an @ prefixed expression, you'll have to check the return value of error_reporting() in your handler. In case it returns zero, you're dealing with a suppressed error.

雨落星ぅ辰 2024-11-24 19:36:30

这是一篇有趣的文章

如何捕获 PHP 致命错误

本页准确讨论了您想要实现的目标。

Here's an Interesting read

How do I catch a PHP Fatal Error

This page discusses exactly what you are trying to achieve.

稍尽春風 2024-11-24 19:36:30

这不是一个很好的答案,但我怀疑这是因为错误“消息”和错误“异常”不同。

php-exceptions-vs-errors 对此说了一些,但我发现的是,根据此评论,看来set_error_handler() 实际上覆盖了@符号。

也就是说,在处理异常时,try、catch 始终是最好的方法。

希望有一点帮助。

This isn't a very good answer, but I suspect it's because error 'messages' and error 'exceptions are not the same.

php-exceptions-vs-errors says a little about this, but what I found was that, as per this comment, it appears that set_error_handler() actually overrides the @ symbol.

That said, try, catch is always the best approach when dealing with exceptions.

Hope that helped a little.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文