[英]Handle exception without using try catch in Zend Framework 1
I have a project which is developed in Zend Framework 1. The project is completed. 我有一个在Zend Framework 1中开发的项目。该项目已完成。
Now while I am testing the whole site, some pages thrown exceptions. 现在,当我测试整个网站时,某些页面引发了异常。 One of such is below: 其中之一如下:
exception 'Zend_Paginator_Exception' with message 'No adapter for type NULL' 消息“没有类型为NULL的适配器”的异常'Zend_Paginator_Exception'
I have searched in net and got the steps to add try-catch to this. 我已经在网上搜索并获得了向其中添加try-catch的步骤。 But it will take much time to check all code and repeat this step. 但是,检查所有代码并重复此步骤将花费大量时间。
Can I write a common exception handler which catches all exceptions and handle it ? 我可以编写一个捕获所有异常并对其进行处理的通用异常处理程序吗?
Zend framework automatically handles exceptions using the errorController. Zend框架使用errorController自动处理异常。 You can enable it by adding the following line in you config file. 您可以通过在配置文件中添加以下行来启用它。
resources.frontController.throwExceptions = 0
If you want to handle exceptions manually then, rather then writing try/catch block all over the code you can centralize it using the code below. 如果要手动处理异常,则可以在下面的代码中将其集中起来,而不是在整个代码中编写try / catch块。
Tell Zend Framework to not handle exceptions. 告诉Zend Framework不处理异常。 Do this in your application.ini 在您的application.ini中执行此操作
resources.frontController.throwExceptions = 1
Define a custom method to handle exceptions in your Bootstrap class. 在Bootstrap类中定义一个自定义方法来处理异常。
public function __handleExceptions(Exception $e){
//render a view with a simple error message for the user
//and send an email with the error to admin
}
Override the _bootstrap()
and run()
methods of Zend_Application_Bootstrap_Bootstrap in your Bootstrap class as shown below. 覆盖Bootstrap类中的Zend_Application_Bootstrap_Bootstrap的_bootstrap()
和run()
方法,如下所示。 This will catch all the exceptions thrown during the bootstrapping and request dispatching process and call your custom exception handler. 这将捕获在引导和请求分配过程中引发的所有异常,并调用您的自定义异常处理程序。
protected function _bootstrap($resource = null)
{
try {
parent::_bootstrap($resource);
} catch(Exception $e) {
$this->__handleExecptions($e);
}
}
public function run()
{
try {
parent::run();
} catch(Exception $e) {
$this->__handleExecptions($e);
}
}
This will eliminate the need of writing try/catch block at multiple places. 这将消除在多个位置编写try / catch块的需要。 Hope this helps. 希望这可以帮助。
If you have this line in your index.php 如果您在index.php中有这一行
$application->bootstrap()->run();
You can wrap it with try
catch
block 你可以用try
catch
块包装它
try {
$application->bootstrap()->run();
} catch (Exception $e) {
//handle all exceptions here
}
Of course you can also have many catch blocks for different types of exceptions. 当然,对于不同类型的异常,您也可以具有许多catch块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.