代码之家  ›  专栏  ›  技术社区  ›  Surfrdan

如何在Zend框架操作中捕获自己的模型异常

  •  0
  • Surfrdan  · 技术社区  · 14 年前

    我有一个引发XMLDocument异常的模型。在我的控制器操作中,我希望包含一个try catch块,该块可能会引发此异常,但由于某些原因,它没有被捕获。

    public function listissuesAction() {
        $publicationPid  = $this->_request->get('publicationPid');
        $this->getFrontController()->throwExceptions(true);
        try {
            $fedoraPublication = new FedoraMETSPublication($publicationPid);
        } catch (XMLDocument_Exception $e) {
            return $this->_forward('content/unavailable');
        }
    }
    

    模型抛出异常如下:

    if ($mets) {
        $this->loadXML($mets);
    } else {
        throw new XMLDocument_Exception('Failed to load METS Document from SOAP Server');
    }   
    

    FedoraMETSPublication扩展了扩展XMLDocument的FedoraMETS。上面的代码在FedoraMETS构造函数的构造函数中。

    我看到以下错误:

    Fatal error:  Uncaught exception 'XMLDocument_Exception' with message 'Failed to load METS Document from SOAP Server' in    2 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS.php:21
    Stack trace:
    #0 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS/Issue.php(16): FedoraMETS->__construct('llgc-id:1183037')
    #1 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS/Publication.php(44): FedoraMETSIssue->__construct('llgc-id:1183037')
    #2 /Users/dof/Sites/ccymod/trunk/application/views/scripts/browse/listissues.phtml(19): FedoraMETSPublication->getIssues()
    #3 /Users/dof/Sites/ccymod/trunk/lib/Zend/View.php(108): include('/Users/dof/Site...')
    #4 /Users/dof/Sites/ccymod/trunk/lib/Zend/View/Abstract.php(831): Zend_View->_run('/Users/dof/Site...')
    #5 /Users/dof/Sites/ccymod/trunk/lib/Zend/Controller/Action/Helper/ViewRenderer.php(903): Zend_View_Abstract->render('browse/listissu...')
    #6 /Users/dof/Sites/ccymod/trunk/lib/Zend/Controller/Action/Helper/ViewRenderer.php(924): Zend_Controller_Action_Helper_ViewRend in /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS.php on line 21
    

    为什么我的动作控制器没有捕捉到这个异常?

    1 回复  |  直到 14 年前
        1
  •  1
  •   netcoder    14 年前

    检查堆栈跟踪,#2:

    #2 /Users/dof/Sites/ccymod/trunk/application/views/scripts/browse/listissues.phtml(19): FedoraMETSPublication->getIssues()
    

    显然,异常被抛出到视图,而不是控制器。使用 try...catch 在视图中(或更改您的方法)。

    推荐文章