array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'yaf-dispatcher.catchexception.php', 1 => 'Yaf_Dispatcher::catchException', ), 'up' => array ( 0 => 'class.yaf-dispatcher.php', 1 => 'Yaf_Dispatcher', ), 'prev' => array ( 0 => 'yaf-dispatcher.autorender.php', 1 => 'Yaf_Dispatcher::autoRender', ), 'next' => array ( 0 => 'yaf-dispatcher.construct.php', 1 => 'Yaf_Dispatcher::__construct', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/yaf/yaf_dispatcher/catchexception.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(Yaf >=1.0.0)
Yaf_Dispatcher::catchException — 开启/关闭自动异常捕获
当 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