Using Exceptions

Zend_Exception is simply the base class for all exceptions thrown within Zend Framework.

Example #1 Catching an Exception

The following code listing demonstrates how to catch an exception thrown in a Zend Framework class:

  1. try {
  2.     // Calling Zend_Loader::loadClass() with a non-existant class will cause
  3.     // an exception to be thrown in Zend_Loader:
  4.     Zend_Loader::loadClass('nonexistantclass');
  5. } catch (Zend_Exception $e) {
  6.     echo "Caught exception: " . get_class($e) . "\n";
  7.     echo "Message: " . $e->getMessage() . "\n";
  8.     // Other code to recover from the error
  9. }

Zend_Exception can be used as a catch-all exception class in a catch block to trap all exceptions thrown by Zend Framework classes. This can be useful when the program can not recover by catching a specific exception type.

The documentation for each Zend Framework component and class will contain specific information on which methods throw exceptions, the circumstances that cause an exception to be thrown, and the various exception types that may be thrown.

blog comments powered by Disqus