Catch database exception in Kohana

Posted by danilo on Stack Overflow See other posts from Stack Overflow or by danilo
Published on 2010-04-18T15:00:46Z Indexed on 2010/04/18 15:03 UTC
Read the original article Hit count: 390

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?

© Stack Overflow or respective owner

Related posts about kohana

Related posts about php