如何在错误的数据库连接上设置自己的Exception?

时间:2021-08-24 11:20:36

When I'm trying to connect to wrong database I'm getting standart kohana exception message like this:

当我试图连接到错误的数据库时,我得到这样的标准kohana异常消息:

Database_Exception [ 1049 ]: Unknown database 'mywrongdatabase'
MODPATH\database\classes\kohana\database\mysql.php [ 108 ]
    protected function _select_db($database)
    {
        if ( ! mysql_select_db($database, $this->_connection))
        {
            // Unable to select database
[line 108]          throw new Database_Exception(':error',
                array(':error' => mysql_error($this->_connection)),
                mysql_errno($this->_connection));
        }

        Database_MySQL::$_current_databases[$this->_connection_id] = $database;

This came out from file

这来自档案

MODPATH\database\classes\kohana\database\mysql.php [ 108 ]

How to set (and where) own message instead of standart kohana's exception? Also I don't want to modify any kohana standart modules (like database) or system files.

如何设置(和在哪里)自己的消息而不是标准kohana的例外?另外,我不想修改任何kohana标准模块(如数据库)或系统文件。

1 个解决方案

#1


1  

You can try with try/catch, but I left both error_log and throw new Exception because I don't know which one you need.

您可以尝试使用try / catch,但我留下了error_log并抛出了新的Exception,因为我不知道您需要哪一个。

try{
    //code with connection
} catch (Exception $e){  
    error_log("This is my own message");
    throw new Exception( 'Something really gone wrong', 0, $e);
} 

#1


1  

You can try with try/catch, but I left both error_log and throw new Exception because I don't know which one you need.

您可以尝试使用try / catch,但我留下了error_log并抛出了新的Exception,因为我不知道您需要哪一个。

try{
    //code with connection
} catch (Exception $e){  
    error_log("This is my own message");
    throw new Exception( 'Something really gone wrong', 0, $e);
}