Search code examples
phperror-handlingtry-catcherror-reporting

How do I manage errors with strict reporting in PHP?


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.


Solution

  • @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.