CHAPTER 7 – EXCEPTIONS – try, catch, and throw

Three language constructs are used by exceptions: try, catch, and throw. To handle an exception, you need to run some code inside a try block, like this: try { $article->display(); } The try block instructs PHP to look out for exceptions generated as the code inside the block is executed. If an exception occurs, it is passed on to one or more catch blocks immediately following the try block: catch (Exception $e) { die($e->getMessage()); } As you can see, the variable $e seems to contain an object. It does-- exceptions are actually objects, the only requirement is that it must be or inherit the Exception class. The Exception class implements a few methods, such as getMessage(), that give you more details about where the origin and cause of the exception. See Chapter 3, "PHP 5 OO Language," the details on the Exception class. To generate an exception in your own code, use the throw statement: $fp = @fopen($filename, "r"); if (!is_resource($fp)) { throw new FileException("could not read '$filename'"); } while ($line = fgets($fp)) { ... In the previous catch example, you saw that the exception was an object. This example creates that object. There is nothing magical about this syntax; throw simply uses the specified object as part of the exception. To semantically separate various types of exceptions, you can define sub- classes of Exception as you see fit: class IO_Exception extends Exception { } class XML_Parser_Exception extends Exception { } class File_Exception extends IO_Exception { } No member variables or methods are required in the exception class; everything that you need is already defined in the built-in Exception class. PHP checks the class names in the catch statement against the exception object with a so-called is_a comparison. That is, if the exception object is an instance of the catch class, or an instance of a subclass, PHP executes the catch block. Here is an example: try { $article->display(); } catch (IO_Exception $e) { print "Some IO problem occurred!"; } catch (XML_Parser_Exception $e) { print "Bad XML input!"; } H e r e, t h e I O _ E x c e p t i o n c a t c h c a t c h e s b o t h I O _ E x c e p t i o n a n d File_Exception, because File_Exception inherits IO_Exception. If every catch fails to capture the exception, the exception goes on to the calling function, giving the calling function the opportunity to catch it. If the exception is not caught anywhere, PHP offers a last chance: the exception-handling function. By default, PHP prints the error message, class name, and a backtrace. By calling set_exception_handler(), you can replace this built-in behavior: <?php function my_exception_handler(Exception $e) { print "Uncaught exception of type " . get_class($e) . "n"; exit; } set_exception_handler("my_exception_handler"); throw new Exception; In this example, my_exception_handler is called for any exception that is not caught inside a catch block. The exception handler function receives the exception object as its single parameter. The exception handler function effec- tively negates the exception, execution will proceed as if the exception was not thrown. Exceptions may not be thrown from within an exception handler function.

SUMMARY In this chapter, you learned about the various types of errors PHP and PEAR can generate and handle. You learned how to customize error handling through php.ini, write your own error handlers, and convert PHP errors to PEAR errors or exceptions. You learned about the problems that may be caused by differences between server back-ends (SAPI modules) and operating systems and some ways of dealing with portability. Finally, you learned how to best use exceptions with PHP and the specif- ics of using exceptions with PEAR. At the time of writing, the PEAR community is still working out how to best introduce and use exceptions with PEAR, so using exceptions with PEAR has been deliberately left out of this edition of this book. Keep an eye on this book's web site at http://php5powerprogramming.com/ for updates!

Post Comment
Login to post comments