我正在尝试在PHP中处理异常但堆栈错误仍然显示而不是由catch处理
I'm calling a method that I know could cause an error and I'm trying to handle the error by wrapping the code in a try/catch statement...
class TestController extends Zend_Controller_Action
{
public function init()
{
// Anything here happens BEFORE the View has rendered
}
public function indexAction()
{
// Anything `echo`ed here is added to the end of the View
$model = new Application_Model_Testing('Mark', 31);
$this->view->sentence = $model->test();
$this->loadDataWhichCouldCauseError();
$this->loadView($model); // this method 'forwards' the Action onto another Controller
}
private function loadDataWhichCouldCauseError()
{
try {
$test = new Application_Model_NonExistent();
} catch (Exception $e) {
echo 'Handle the error';
}
}
private function loadView($model)
{
// Let's pretend we have loads of Models that require different Views
switch (get_class($model)) {
case 'Application_Model_Testing':
// Controller's have a `_forward` method to pass the Action onto another Controller
// The following line forwards to an `indexAction` within the `BlahController`
// It also passes some data onto the `BlahController`
$this->_forward('index', 'blah', null, array('data' => 'some data'));
break;
}
}
}
...but the problem I have is that the error isn't being handled. When viewing the application I get the following error...
( ! ) Fatal error: Class 'Application_Model_NonExistent' not found in /Library/WebServer/Documents/ZendTest/application/controllers/TestController.php on line 23
Can any one explain why this is happening and how I can get it to work?
Thanks
我正在调用一个我知道可能导致错误的方法,我试图通过包装来处理错误 try / catch语句中的代码... p>
p>
类TestController扩展Zend_Controller_Action
{
公共函数init ()
{
//此处发生的任何事情在视图渲染之前发生
}
公共函数indexAction()
{
//这里的任何`echo`都被添加到视图的末尾
$ model = new Application_Model_Testing('Mark',31);
$ this-> view-> sentence = $ model-> test();
$ this-> loadDataWhichCouldCauseError();
$这 - >的loadView($模型); //此方法将Action转发到另一个Controller
}
私有函数loadDataWhichCouldCauseError()
{
try {
$ test = new Application_Model_NonExistent();
} catch(Exception $ e) {
echo'handle the error';
}
}
私有函数loadView($ model)
{
//让我们假设我们有大量需要不同Views
开关的模型(get_class) ($ model)){
case'Application_Model_Testing':
// Controller有一个`_forward`方法将Action传递给另一个Controller
//以下行转发到`BlahController`中的`indexAction` \ n //它还将一些数据传递到`BlahController`
$ this-> _forward('index','blah',null,array('data'=>'some data'));
break ;
}
}
}
code> pre>
...但我遇到的问题是没有处理错误。 查看应用程序时,我收到以下错误... p>
(!)致命错误:/ Library / WebServer / Documents / ZendTest / application /中找不到类'Application_Model_NonExistent' 第23行上的controllers / TestController.php
code> pre>
任何人都可以解释为什么会这样,以及如何让它工作? p>
谢谢 p>
div>
use
if (class_exists('Application_Model_NonExistent')) {
$test = new Application_Model_NonExistent;
} else {
echo 'class not found.';
}
like @prodigitalson said you can't catch that fatal error.
Thats not an exception, thats a FATAL
error meaning you cant catch it like that. By definition a FATAL
should not be recoverable.
An error and an exception are not the same thing. Exceptions are thrown and meant to be caught, where errors are generally unrecoverable and triggered with http://www.php.net/manual/en/function.trigger-error.php
If you need to do some cleanup because of an error, you can use http://www.php.net/manual/en/function.set-error-handler.php
Exception and Error are different things. There is an Exception class, which you are using and that $e is it's object.
You want to handle errors, check error handling in php-zend framework. But here, this is a Fatal error, you must rectify it, can not be handled.