Yaf_Dispatcher::catchException
(Since Yaf 1.0.0.5)
public boolean Yaf_Dispatcher::catchException( boolean $switch );
在ap.dispatcher.throwException开启的状态下, 是否启用默认捕获异常机制
当然,也可以在配置文件中使用ap.dispatcher.catchException=$switch达到同样的效果, 默认的是开启状态.
例 11.37. Yaf_Dispatcher::catchException的例子
<?php
$app = new Yaf_Application("conf.ini");
/**
* 开启捕获异常
*/
Yaf_Dispatcher::getInstance()->catchException(TRUE);
?>
异常捕获开启以后, 需要定义Error Controller的Error Action来接管未捕获的异常:
例 11.38. Yaf_Dispatcher::catchException的ErrorController例子
<?php
/* 如果你定义了如下的 ErrorController */
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;
}
}
}
?>
/* 现在如果发生了错误, 比如访问了一个不存在的控制器: */
404:Could not find controller script **/application/controllers/No-exists-controller.php