3

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.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174

3 Answers3

4

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.

svens
  • 11,438
  • 6
  • 36
  • 55
  • Spot on sir. All my error handler does is throw an `ErrorException`; I needed to filter the silenced errors. – Dan Lugg Jun 30 '11 at 19:13
2

Here's an Interesting read

How do I catch a PHP Fatal Error

This page discusses exactly what you are trying to achieve.

Community
  • 1
  • 1
Pheonix
  • 6,049
  • 6
  • 30
  • 48
2

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.

Community
  • 1
  • 1
Drazisil
  • 3,070
  • 4
  • 33
  • 53