Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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
Oop 处理异常并从服务层返回响应对象_Oop_Zend Framework_Design Patterns_Exception_Exception Handling - Fatal编程技术网

Oop 处理异常并从服务层返回响应对象

Oop 处理异常并从服务层返回响应对象,oop,zend-framework,design-patterns,exception,exception-handling,Oop,Zend Framework,Design Patterns,Exception,Exception Handling,我有一个服务层,负责处理异常 问题是,我是否应该在服务层处理异常,以及如何将适当的异常消息传递给视图 class App_Service_Feed { function create() { //... try { $feed->getMapper->insert(); } catch (Zend_Db_Statement_Exception $e) { //what do I return here? }

我有一个服务层,负责处理异常

问题是,我是否应该在服务层处理异常,以及如何将适当的异常消息传递给视图

class App_Service_Feed {
  function create() {
    //...
    try {
      $feed->getMapper->insert();
    } catch (Zend_Db_Statement_Exception $e) {
      //what do I return here?        
    } catch (Exception $e) {
      //what do I return here?
    }
  }
}
我正在考虑返回一个具有某种描述的响应对象,以便在我的控制器中操作它

class App_Controller {
  //...
  $response = $service->create();
  if($response->status) {

  }
}

或者,我想知道是否在控制器中处理异常

您需要做的就是抛出异常,以便Zend Front controller稍后捕获它

class App_Service_Feed {
  function create() {
    //...
    try {
      $feed->getMapper->insert();
    } catch (Zend_Db_Statement_Exception $e) {
      throw new Zend_Exception("my own message");      
    } catch (Exception $e) {
      throw new Zend_Exception("my different message");
    }
  }
}

甚至比jason bourne的方式更好(是的):

为什么这样更好

  • 您正在使用自己的异常类(扩展Zend_异常)。因此,您可以立即看到异常抛出的位置,并且可以构建自己的附加检查,等等
  • 您正在传递最后一个异常,以获得有关异常的更多历史信息(跟踪)
实现异常的最佳方法是拥有扩展异常类的层次结构

App_Exception extends Zend_Exception
App_Service_Exception extends App_Exception
App_Service_Feed_Exception extends App_Service_Exception

所以每个文件夹都包含一个Exception.php。通过这种方式,您可以捕获并重新显示每一级别的异常(如有必要)。

您可以遵循这种方法,我通常在执行异常处理时遵循这种方法:

class App_Service_Feed {


function create() throws CustomException, OtherCustomException {
    //...
    try {
      $feed->getMapper->insert();
    } catch (Zend_Db_Statement_Exception $e) {
      //you can throw ur custom exception here. 
      //By doing so you can increase its functionality and understand what is the problem
       throw new CustomException();       
    } catch (Exception $e) {
      //here u can check some general exception like NullPointer, IOException etc(related 
      //to ur case) using instanceof.
      throw new OtherCustomException

    }
  }
}
现在,在控制器中,您需要处理此异常并显示一些消息:-

class App_Controller {
  //...
  App_Service_Feed obj = new App_Service_Feed();
  try{  
   obj.create()
  }catch(CustomException c)
   {
     //display message
   }catch(OtherCustomException o)
    {
      //display other message
    }
  }
}

+1.对问题的详细描述。
class App_Controller {
  //...
  App_Service_Feed obj = new App_Service_Feed();
  try{  
   obj.create()
  }catch(CustomException c)
   {
     //display message
   }catch(OtherCustomException o)
    {
      //display other message
    }
  }
}