25 Nov
Posted by Ganesh H S , Bangalore, India as zend framework
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
2 Responses
Nils
June 17th, 2009 at 7:30 am
1catch (Zend_Exception $e) {
{
echo “Db error : ” . $e->getMessage() . “\n”;
}
Should be:
catch (Zend_Exception $e) {
echo “Db error : ” . $e->getMessage() . “\n”;
}
Ganesh H S , Bangalore, India
August 4th, 2009 at 4:23 pm
2Nils,
I have corrected the typo error.
Thanks,
Ganesh
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments