任何异常的自定义错误处理程序

时间:2012-12-02 21:27:47

标签: php exception-handling yii

问题是我根据这个主题http://www.yiiframework.com/doc/guide/1.1/en/topics.error做了所有事情,但是当我在我的控制器中引发异常(抛出新的异常(“错误”);)时,yii不会路由到我的自定义错误控制器并使用系统默认。如何处理此类异常?

2 个答案:

答案 0 :(得分:2)

您必须使用Yii自己的异常类之一:CExceptionCDbExceptionCHttpException。从您提供的链接:http://www.yiiframework.com/doc/guide/1.1/en/topics.error#raising-exceptions

// if post ID is invalid
throw new CHttpException(404,'The specified post cannot be found.');

答案 1 :(得分:1)

如果mysql异常,请将您的查询作为

运行
$insertCommand = $model->getCommandBuilder()->createSqlCommand($sql,$baseParams+$relParams);
try {
        $insertCommand->execute();
} catch(CDbException $e) {
        // Here's a way to get to the error code for the statement in question.
        // These codes are standardized ANSI SQL "SQLSTATE" error codes.
        $sqlErrorCode = $insertCommand->pdoStatement->errorCode();
        // And to get to the class part of it, simple grab the two first characters.
        // The class should be the same regardless of DB vendor, while the rest of the code can differ.
        // For example one particular error was reported by PostgreSQL as 23505 but MySQL only said 23000.
        $sqlErrorCodeClass = substr($sqlErrorCode, 0, 2);
}