开发者

PHP error reporting and mail()

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\db开发者_如何转开发c_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.


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.


Here's an Interesting read

How do I catch a PHP Fatal Error

This page discusses exactly what you are trying to achieve.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜