Yaf_Dispatcher::catchException

(Yaf >=1.0.0)

Yaf_Dispatcher::catchException开启/关闭自动异常捕获

说明

public Yaf_Dispatcher::catchException(bool $flag = ?): Yaf_Dispatcher

当 application.dispatcher.throwException 开启的时候(也可以通过调用 Yaf_Dispatcher::throwException(TRUE)() 来开启),Yaf 会在错误发生时抛出 Exception 而不是触发错误。

如果开启了 Yaf_Dispatcher::catchException() (可以通过设置 application.dispatcher.catchException 来开启),如果已经定义,所有未捕获的 Exception 都将被 ErrorController::error 捕获。

参数

flag

bool

返回值

示例

示例 #1 Yaf_Dispatcher::catchException() 示例

/* if you defined a ErrorController like following */
<?php
class ErrorController extends Yaf_Controller_Abstract {
/**
* you can also call to Yaf_Request_Abstract::getException to get the
* un-caught exception.
*/
public function errorAction($exception) {
/* error occurs */
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

参见

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top