【发布时间】:2013-01-12 19:19:49
【问题描述】:
最近,我正在学习 PHP 和 CodeIgniter 框架。我正在处理异常处理,但有一件事让我感到惊讶,当database.php 设置中有db_debug = TRUE 时,异常处理不适用于数据库错误。
如果是db_debug = FALSE,我可以抛出并捕获错误,但是当它是TRUE时,它会直接显示有数据库错误的页面。
这是我的代码:
在Model:
$this->db->insert('tbl_name',$data);
if (strpos($this->db->_error_message(),'constraint_name') !== FALSE)
{
throw new UniqueConstraintException($this->db->_error_message()); // UniqueConstraintException is customized excpetion
}
在Controller:
try
{
//calling the method from model having above code
}
catch(UniqueConstraintException $ex)
{
echo "There is already item with same item name";
}
catch(Exception $ex)
{
echo $ex->getMessage();
}
即使有db_debug = TRUE设置,是否可以对数据库错误进行异常处理?
【问题讨论】:
标签: php codeigniter exception-handling codeigniter-2