【问题标题】:How to set an own Exception on wrong database connection?如何在错误的数据库连接上设置自己的异常?
【发布时间】:2013-05-09 05:21:14
【问题描述】:

当我尝试连接到错误的数据库时,我会收到如下标准的 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;

这来自文件

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

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

【问题讨论】:

    标签: php database exception kohana kohana-3.2


    【解决方案1】:

    您可以尝试使用 try/catch,但我留下了 error_log 和 throw new Exception,因为我不知道您需要哪个。

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

    【讨论】:

    • 我在哪里可以做到这一点?还是只能在模块文件中?
    • 我在 SO 上找到了这个问题和答案——你可以试试:stackoverflow.com/questions/6831402/…
    • 好吧,我发现我真的可以像他一样覆盖 kohana 异常类,但不能覆盖自定义错误,因为在我的示例中我有 Database_Exception 这可能是很多事情,比如错误db 名称或用户/密码数据。而不是他在他的例子中所拥有的:strikt HTTP_Exception_404 错误句柄。
    • 也许可以在case 块中检查这个...嗯
    • 代码中的某处必须有“Database_Exception”。尝试搜索它并进行与 HTTP_Exception 相同的操作。
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多