Should I use a try catch in following code snippet. I think if zend can’t delete a row or exception it will return integer (0). So what is better and why?
$statusDbObj = new Tracker_Model_DbTable_Status();
$where = $statusDbObj->getAdapter()->quoteInto('tracking_number = ?', $airwaybill_number);
$delete = $statusDbObj->delete($where);
if($delete > 0)
{
// do something
}
else
{
// not deleted
}
Here is with try catch:
try
{
$statusDbObj = new Tracker_Model_DbTable_Status();
$where = $statusDbObj->getAdapter()->quoteInto('tracking_number = ?', $airwaybill_number);
$delete = $statusDbObj->delete($where);
if($delete > 0)
{
// do something
}
}
catch (Exception $e)
{
// not deleted and print error
}
I am new in using try catch
It depends on what kind of issues you want to address:
deleteresult will help you detect “expected issues” (i.e., when you couldn’t delete entries because they didn’t match thewhereexpression).try–catchstructure (for instance, catching aZend_Db_Exception, which is likely to be thrown by thedeletemethod), you’ll be also able to detect “unexpected issues” (for instance, if the DB connection failed). If you omit this structure, eventual exceptions will pop up to the caller of your code, and you’ll also need to address them somewhere.Hope that helps,