问题是我按照这个主题做所有事情http://www.yiiframework.com/doc/guide/1.1/en/topics.error,但是在我的控制器中我引发异常( throw new Exception("error"); ) , yii 不要路由到我的自定义错误控制器并使用系统默认值。如何处理此类异常?
问问题
2450 次
2 回答
3
你必须使用 Yii 自己的异常类之一:CException
,CDbException
或CHttpException
. 从您提供的链接: 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.');
于 2012-12-02T22:01:13.987 回答
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);
}
于 2012-12-03T08:43:07.873 回答