Search code examples
phpzend-frameworkexceptionzend-framework2

Exception Handling In Zend Framework 2


I have tried using try catch in my code but it's not catching any exceptions.

{           
   $this->mapper->DeletePassssedAndImages($langcode."_topic",$id,$type,$langcode);
}
catch ( \Exception $exc )
{
    echo "hi";
    exit;
);

Its not going to the catch block even if i throw an exception manually.


Solution

  • Zend Framework throw the Exceptions more specific. So, you will know from where the error come from. Example, in Zend\ServiceManager there are a few exceptions

    Zend\ServiceManager\Exception\CircularDependencyFoundException
    Zend\ServiceManager\Exception\CircularReferenceException
    Zend\ServiceManager\Exception\InvalidArgumentException
    Zend\ServiceManager\Exception\InvalidServiceNameException
    Zend\ServiceManager\Exception\RuntimeException
    Zend\ServiceManager\Exception\ServiceNotCreatedException
    Zend\ServiceManager\Exception\ServiceNotFoundException
    

    You can make handler based on Exception throwed. But you can use try catch block like this to handle Exception.

    try {
    
    } catch \Exception ($e) {
    
    }
    

    Because all of those exceptions extends from \Exception