Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework ZF2中try/catch的工作原理_Zend Framework_Zend Form_Zend Db - Fatal编程技术网

Zend framework ZF2中try/catch的工作原理

Zend framework ZF2中try/catch的工作原理,zend-framework,zend-form,zend-db,Zend Framework,Zend Form,Zend Db,从zend文档中,我了解到可以实现try-catch。当我使用zend exception时,即使尝试有效,也无法捕获它 try { loadClass() with a non-existant class will cause an exception to be thrown in Zend_Loader: Zend_Loader::loadClass('nonexistantclass'); } catch (Zend_Exception $e) {

从zend文档中,我了解到可以实现try-catch。当我使用zend exception时,即使尝试有效,也无法捕获它

try { 
    loadClass() with a non-existant class will cause an exception 
    to be thrown in Zend_Loader: 
    Zend_Loader::loadClass('nonexistantclass'); 
} catch (Zend_Exception $e) { 
    echo "Caught exception"; 
    // Other code to recover from the error 
}
错误:致命错误:在第22行的C:\wamp\www\zf\module\Album\src\Album\Controller\AlbumController.php中找不到类'Album\Controller\Zend\Loader\Loader' 显示捕获未发生错误消息

编辑

但是当我抛出下面代码中的异常时,我得到的消息是错误

try { throw new \Exception("My exception"); } catch (Exception $e) { echo "Caught exception $e\n"; exit; }

这里有几个问题。尽管有代码示例,但该错误表明您正在使用ZF2;这个错误是一个PHP致命错误,而不是一个异常,这就是为什么您的try/catch无法工作的原因。ZF2中没有Zend_加载器,因此PHP无法找到它

我建议只使用标准PHP函数
class\u exists()

if (class_exists('Some\Class')) {
    ...
} else {
    ...
}

这会让你实现你想要做的。无需担心异常。

请尝试改进问题的格式。请同时包含您遇到的错误。另外,您已经用ZF2标记了这个问题,但您的代码是ZF1代码。要创建一个错误管理器,您需要了解try catch的工作原理。下面的文档@TimFountain说这个代码在zend2中。谢谢您的帮助[link]()@TimFountain:您能看看我的评论并帮助我吗