[英]Working of try / catch in ZF2
从zend文档中,我了解到可以实现try catch。 当我使用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
}
错误 :致命错误:在第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_Loader
,因此PHP将无法找到它。
我建议只使用标准的PHP函数class_exists()
代替:
if (class_exists('Some\Class')) {
...
} else {
...
}
这应该可以让您实现想要做的事情。 无需担心异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.