As in other object oriented programming languages, zend framework supports try catch, all exceptions thrown by Zend Framework classes should throw an exception that derives from the base class Zend_Exception.

Example 1:

In this example, code require_once “Zend_Loader.php”; is commented, so error is thrown when we are trying to access Zend_Loader.

<?php
//require_once "Zend_Loader.php";

try
{
Zend_Loader::loadClass('Zend_Session');
}
catch (Zend_Exception $e) {
echo "Error message: " . $e->getMessage() . "\n";
}
?>

Example 2:
In this example, $sql does not have a closing double quote, so the error thrown is catched and is printed.

 <?php

/**
* Zend Loader
*/
require_once "Zend/Loader.php";
Zend_Loader::loadClass('Zend_Db');
Zend_Loader::loadClass('Zend_Config_Ini');

/**
* Set config
*/
$config = new Zend_Config_Ini("../application/config.ini", "staging");

/**
* Set the database
*/

// setup database
$db = Zend_Db::factory($config->db->adapter,
$config->db->config->toArray());

try

{

$sql = "SELECT count(*) FROM TABLE_NAME";

$db->fetchOne($sql) ;

}

catch (Zend_Exception $e) {

{

echo "Db error : " . $e->getMessage() . "\n";

}

?>

Reference Links:
http://ganeshhs.com/zend-framework/zend-framework-tutorial-part-4-zend-config
http://ganeshhs.com/zend-framework/zend-framework-tutorial-part-3-zend-loader

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Simpy
  • StumbleUpon
  • Technorati
  • YahooMyWeb