PHP - Manual: Yaf_Dispatcher::catchException
2024-12-22
(Yaf >=1.0.0)
Yaf_Dispatcher::catchException — 开启/关闭自动异常捕获功能
当 application.dispatcher.throwException 开启的时候(你也可以通过调用 Yaf_Dispatcher::throwException(TRUE)() 来开启它),Yaf将会抛出异常而不是触发异常发生。
如果开启了 Yaf_Dispatcher::catchException() (可以通过设置application.dispatcher.catchException来开启),并且在你定义了异常处理的controller的情况下,Yaf会将所有未捕获的异常交给Error Controller的Error Action来处理。
flag
bool
示例 #1 Yaf_Dispatcher::catchException()example
/* 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 error($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
官方地址:https://www.php.net/manual/en/yaf-dispatcher.catchexception.php