Php 任何异常的自定义错误处理程序
问题是我是根据这个主题来做的,但当我在控制器中引发异常(抛出新异常(“错误”);)时,我不会路由到我的自定义错误控制器并使用系统默认值。如何处理此类异常?您必须使用Yii自己的异常类之一:Php 任何异常的自定义错误处理程序,php,exception-handling,yii,Php,Exception Handling,Yii,问题是我是根据这个主题来做的,但当我在控制器中引发异常(抛出新异常(“错误”);)时,我不会路由到我的自定义错误控制器并使用系统默认值。如何处理此类异常?您必须使用Yii自己的异常类之一:ceException、cdbeException或CHttpException。从您提供的链接: 如果mysql出现异常,请按以下方式运行查询 $insertCommand = $model->getCommandBuilder()->createSqlCommand($sql,$basePara
ceException
、cdbeException
或CHttpException
。从您提供的链接:
如果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);
}
我知道这种情况,但如果从mysql抛出异常(例如错误的查询或类似的smth)?并且对于每个查询,我必须编写try-catch?
$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);
}