drby30217 2010-04-18 15:00
浏览 49
已采纳

在Kohana中捕获数据库异常

I'm using Kohana 2. I would like to catch a database exception to prevent an error page when no connection to the server can be established.

The error displayed is

system/libraries/drivers/Database/Mysql.php [61]:

mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at
'reading initial communication packet', system error: 110

The database server is not reachable at all at this point.

I'm doing this from a model. I tried both

public function __construct()
{
    // load database library into $this->db
    try
    {
        parent::__construct();
    }
    catch (Exception $e)
    {
        die('Database error occured');
    }
}

as well as

try
{
    $hoststatus = $this->db->query('SELECT x FROM y WHERE z;');
}
catch (Exception $e)
{
    die('Database error occured');
}

...but none of them seemed to work. It seems as if no exception gets passed on from the main model. Is there another way to catch the database error and use my own error handling?

  • 写回答

3条回答 默认 最新

  • drtkyykai004574380 2010-05-06 08:52
    关注

    Kohana 2 does not convert errors into exceptions. You will either need to attach your own error handler, or use error_reporting() to turn off the error (temporarily) then do some kind of handling yourself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题