Click to See Complete Forum and Search --> : Exceptions and Error Handling


visualAd
09-26-2005, 11:49 AM
On my latest site I have made use of exceptions in an ODBC wrapper class which I have written. My class will throw and exception if a query fails. I have also set a custom error handler function which catches all warnings and notices and logs them in a file.

The problem is when a query fails, the ODBC function which executes it also generates a warning, which is then logged. As I have already thrown an exception, I would like to disable this.

At present, my error handling class is testing the error_reporting PHP setting to determine whether an error should be logged. So plan to disable the warning by lowering the error level before executing each query and returning it back to what it was after. I would however like to know if there is a more elegant way of doing this.

Thanks

Ps: I've included the code for both the error handling class and the odbc wrapper class for reference.

Shrike
09-30-2005, 09:27 AM
To my knowledge there is no way turning off errors in the way you want. From what I can see you have a custom error handler set with set_error_handler() for logging of errors, and presumably you raise standard PHP errors on catching an Exception which then also get logged. Is it an option to only log the Exception messages rather than raising PHP errors and using set_error_handler()? I can't see any other way around other than the one you suggest.

I think PHP's Exceptions are really aimed at application level errors (i.e. badly formed data etc) rather than system level ones (i.e. PHP native functions failing).

visualAd
09-30-2005, 10:08 AM
I have actually managed to solve this. There is a bit I missed in the documentation:

It is important to remember that the standard PHP error handler is completely bypassed. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.


The way I am doing it is to set a top level exception handler for any uncaught exceptions and have them fire a user error which kills the script, logs the error and dispalays an error page.

This is my first experience with the using exceptions and I must say they are very powerful. I have heard some opinions that they are white elephant and encourage spaghetti coding beucase they cause unconditional jumps like goto statements do. But as long as you don't use them to execute huge chunks of code, they are very powerful and useful.