不能在codeIgniter中捕获异常


can't catch exception in codeIgniter

对不起,我的英语不好,但我需要帮助我有一个问题,当我试图捕捉插入查询

我有一个表employee,有3个主键(rule, CIN, name)

当我添加一个具有重复值的新员工时,我想捕获异常

My Model

  function insert_Employe($donnees){
   try{
    $this->db->insert('Employe',$donnees);
    return 'employe enregistré avec succes';
   }catch(Exception $e)
   {
       return $e->getMessage();
   }
}

我的控制器

  function nouveau(){
    if(!$this->offres->_isLogged() || !$this->input->post('Ajax')==1){
        redirect(base_url().'users');
    }
    $values = $this->_get_values();
    try{
    $message = $this->employe->insert_Employe($values);
    echo $message;
    }catch(Exception $e){
        echo $e->getMessage();
    }
}

i cannot catch the exception

这永远不会抛出异常:

$this->db->insert('Employe',$donnees);

所以你永远捕捉不到这样的异常。如果insert()方法失败,如果$db['default']['db_debug']在数据库上设置为TRUE,它将生成显示自定义错误。配置,否则返回FALSE。

最好的办法是在执行DB操作时检查错误:

$error = $this->db->_error_message()
if(!empty($error)) {
  throw new Exception('Your message');
}
相关文章: