0

I use a custom error handler with complete error reporting in PHP for tracking errors. This works great for debugging and logs all my errors in a database to investigate later.

Anyway, this method now disables the usage of @ to ignore an error when one occurs. I now have the issue where I try to rename a directory on my system, because it may occasionally throw an error (if files are being accessed within it).

I would like to be able to catch this error in my code, to prevent executing the rest of the function, but I also do not want this error to appear in my error logging database (considering this error is 'managed' within the code, there is no need to see that it failed).

Is there a simple solution to this? I try using try / catch but it still appears to throw the error.

1
  • Can I see your try/catch block? Commented Apr 20, 2011 at 5:27

2 Answers 2

1

You can convert all errors/warnings/notices to exceptions

function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}
set_error_handler('exceptions_error_handler');

I think it is better to handle exceptions, than php native errors.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm, thank you. This is progress, but now all errors need to use try/catch is that correct? My function no longer logs the errors to the database because this exception is thrown, so if I don't use try/catch everywhere in my scripts these errors aren't logged.
@Rhys: it depends on what you're trying to achieve. In my apps if error is really unexpected, then it is being caught in the global try-catch wrapper, where it is logged and where I echo some kind of Sorry-page to user. If error is expected (like when we got unique constraint error in database) - then I handle it in-place and don't log (because it need not to be logged, since it is one of "expected" errors)
1

@zerkms' solution would work fine, but my error handler is already completed so to extend this to give me the functionality, I have simply included:

if ( error_reporting() == 0 )
  return;

at the start of my handler. This way, if the @ is used on a function, the error is still thrown, but ignored at the start of the handler (hence, not logged into the database, etc) and I will still get the boolean result from a function, such as rename().

This also still enabled me to use a try/catch solution on code if need be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.