CHAPTER 7 – Handling PEAR Errors

The default behavior for PEAR errors is to do nothing but return the object. However, it is possible to set an error mode that will be used for all consequent errors raised. The error mode is checked when the PEAR_Error object is cre- ated, and is expressed by a constant: <?php require_once 'DB.php'; PEAR::setErrorHandling(PEAR_ERROR_DIE, "Aborting: %sn"); $dbh = DB::connect('mysql://test@localhost/test'); print "DB::connect ok!n"; ?> This previous example is simplified here by using a global default error handler that applies to every PEAR error that has no other error mode config- ured. In this case, we use PEAR_ERROR_DIE, which prints the error message using the parameter as printf format string, and then die. The advantage of this approach is that you can code without checking errors for everything. It is not very graceful, but as you will see later in the chapter, you may also apply temporary error modes during operations that need more graceful handling.

Post Comment
Login to post comments