問題自定義錯誤處理程序是,我都按照這個話題http://www.yiiframework.com/doc/guide/1.1/en/topics.error,但是當我在控制器I引發異常(拋出新的異常(「錯誤」);),警予不路由到我的自定義錯誤控制器並使用系統默認值。如何處理這種例外情況?任何例外
Q
任何例外
2
A
回答
2
您必須使用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.');
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);
}
+0
對於每一個查詢,我必須寫試試catch? –
相關問題
- 1. 任務:例外和取消
- 2. 以任何代價捕捉例外或避免例外情況會更好嗎?
- 3. 即使我完全信任WebPermission例外
- 4. 處理任務工廠例外
- 5. AppEngine「任務規模太大」例外
- 6. LiteralControlBuilder任何示例?
- 7. 例外 - 什麼是「例外」?
- 8. 在Task.WaitAll中處理取消的任務和任務例外?
- 9. TibcoBW如何例外
- 10. ASP.NET catch {}節沒有任何例外發射
- 11. Python:Tornado ioloop在KeyboardInterrupt上沒有任何例外被殺死
- 12. gradle cucumber jvm例外:在[classpath:]找不到任何功能
- 13. ServiceController只是卡住沒有任何例外
- 14. PHP:線路評論的任何例外情況?
- 15. 例外:'str'對象沒有任何屬性'text'
- 16. 例外,在我想調試沒有任何棧展開
- 17. 小工具:沒有任何例外,小工具無法打開
- 18. 遊戲無法以快速啓動,但沒有任何例外
- 19. LINQ查詢使用。任何條件返回nullreference例外
- 20. Apache CXF - 無法滿足任何策略替代例外
- 21. 任何人都沒有例外地在iOS 9上工作NSURLConnection?
- 22. 蟒正則表達式拆分任何\ W +有一些例外
- 23. 無任何例外的被禁止下載
- 24. Flask應用程序引發500錯誤,沒有任何例外
- 25. 例外:「無法連接到任何指定的mysql主機」
- 26. 硒隨機超時例外沒有任何消息
- 27. 例外:無法使用任何種子八卦
- 28. 在堆上分配例外的任何缺陷?
- 29. 任何方式從.dll中捕獲例外.net
- 30. 剃刀視圖引擎 - 調用任何函數時的例外
我知道這個情況,但如果異常從MySQL拋出(例如錯誤的查詢或不服像那)? –