我已使用ZF 2.0.3
设置ZendSkeletonApplication,我无法在控制器中捕获异常。例如,如果我将下面的代码放在module/Application/src/Application/Controller/IndexController.php
:
public function indexAction() {
echo "BEFORE\n";
try {
throw new \Exception("My exception");
} catch (Exception $e) {
echo "Caught exception $e\n";
exit;
}
并访问我获得的页面:
BEFORE
An error occurred
An error occurred during execution; please try again later.
Additional information:
Exception
File:
module/Application/src/Application/Controller/IndexController.php:25
Message:
My exception
ViewModel
启动并显示异常,有效阻止我抓住它。
如何在ZF2
控制器中捕获异常?
答案 0 :(得分:33)
您正在抛出PHP的通用异常
throw new \Exception("My exception");
但是你从当前的命名空间中捕获了Exception
} catch (Exception $e) {
假设您的控制器在Application\Controller
,您必须声明
use \Exception;
在您的班级上方import the global Exception进入当前名称空间或
} catch (\Exception $e) {
捕获PHP的全局异常。