[英]Working of try / catch in ZF2
From the zend documentation i have learned that the try catch can be implemented. 从zend文档中,我了解到可以实现try catch。 when i use zend exception,it cannot be caught even though the try is working 当我使用zend异常时,即使尝试有效也无法捕获
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
}
ERROR :Fatal error: Class 'Album\\Controller\\Zend\\Loader\\Loader' not found in C:\\wamp\\www\\zf\\module\\Album\\src\\Album\\Controller\\AlbumController.php on line 22 The catch is not happening error message is being shown 错误 :致命错误:在第22行的C:\\ wamp \\ www \\ zf \\ module \\ Album \\ src \\ Album \\ Controller \\ AlbumController.php中找不到类'Album \\ Controller \\ Zend \\ Loader \\ Loader'显示错误信息
EDIT 编辑
But when i throw the exception as in the following code,i get the the message as error. 但是当我按照以下代码抛出异常时,我得到的消息是错误。
try { throw new \Exception("My exception"); } catch (Exception $e) { echo "Caught exception $e\n"; exit; }
There are a few issues here. 这里有一些问题。 Despite your code example, the error suggests you are using ZF2; 尽管有代码示例,但该错误表明您正在使用ZF2。 and the error is a PHP fatal error, not an exception, which is why your try/catch doesn't work. 并且该错误是PHP致命错误,而不是异常,这就是为什么您的try / catch无法正常工作的原因。 There is no Zend_Loader
in ZF2, so PHP won't be able to find that. ZF2中没有Zend_Loader
,因此PHP将无法找到它。
I'd suggest just using the standard PHP function class_exists()
instead: 我建议只使用标准的PHP函数class_exists()
代替:
if (class_exists('Some\Class')) {
...
} else {
...
}
which should let you achieve what you're trying to do. 这应该可以让您实现想要做的事情。 No need to worry about exceptions. 无需担心异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.