(Yaf >=1.0.0)
Yaf_Dispatcher::catchException — 開啟/關閉例外捕捉
當 application.dispatcher.throwException 設為開啟時(您也可以呼叫 Yaf_Dispatcher::throwException(TRUE)() 來啟用它),Yaf 會在發生錯誤時拋出例外,而不是觸發錯誤。
如果啟用 Yaf_Dispatcher::catchException()(也可以通過設定 application.dispatcher.catchException 來啟用),所有未捕獲的例外都會被 ErrorController::error 捕獲,前提是你有定義這個控制器。
flag
布林值
範例 #1 Yaf_Dispatcher::catchException() 範例
/* 如果你定義了一個像下面這樣的 ErrorController */
<?php
class ErrorController extends Yaf_Controller_Abstract {
/**
* 你也可以呼叫 Yaf_Request_Abstract::getException 來取得未捕獲的例外。
*/
public function errorAction($exception) {
/* 發生錯誤 */
switch ($exception->getCode()) {
case YAF_ERR_NOTFOUND_MODULE:
case YAF_ERR_NOTFOUND_CONTROLLER:
case YAF_ERR_NOTFOUND_ACTION:
case YAF_ERR_NOTFOUND_VIEW:
echo 404, ":", $exception->getMessage();
break;
default :
$message = $exception->getMessage();
echo 0, ":", $exception->getMessage();
break;
}
}
}
?>
上述範例會輸出類似以下的內容
/* now if some error occur, assuming access a non-exists controller(or you can throw a exception yourself): */ 404:Could not find controller script **/application/controllers/No-exists-controller.php