Now, while the error-handling technique described on the previous page works great so long as you only have a couple of queries running in each script, it can get tedious if you need to execute eight, ten or a hundred queries within a script. Which is why the abstraction layer also provides a generic way to trap and resolve errors, via a default error handler. This default error handling mechanism can be defined through the setErrorHandling() method, and it accepts any one of the following five parameters: PEAR_ERROR_RETURN - do nothing PEAR_ERROR_PRINT - print the error message but continue executing the script PEAR_ERROR_TRIGGER - trigger a PHP error PEAR_ERROR_DIE - terminate script execution PEAR_ERROR_CALLBACK - call another function to handle the error Let's see this in action: In this case, the third query contains a syntax error (a missing quote). When this error is encountered, an exception is raised and the error handler is invoked to handle the error. Since I've specified an error message will appear, but the script will move on to execute the fourth query. Here's the output: In case you're wondering, the second argument to the setErrorHandling() method above is an optional string that can be used to apply basic formatting to the error message. You can omit it if you like - the output will look just as pretty without it. You can also specify a callback function of your own to handle the error, in case you want to do something complicated with it - for example, insert it into a table, or log it to a file. In such a situation, the setErrorHandling() function must be passed two arguments: the constant PEAR_ERROR_CALLBACK, and the name of your user-defined error handler. If an error occurs, this error handler is invoked with an object as argument; this object represents the error, and its properties hold detailed information on what went wrong. If all this sounds a little complicated, the next example might make it easier to understand: In this case, I'm logging every error that occurs to a log file using my own little catchMe() function. This function receives an object when an error occurs; this object can be manipulated to expose detailed information about the error (including the raw error message returned by the server), and this information can then be written to a file.
blog comments powered by Disqus |