Zend Framework 2 – catching an exception

It sounds trivial, but I didn’t know or care before: Zend Framework has its own errorhandling and when I tried to catch an exception with try and catch(Exception $e), that didn’t do anything. Why? Because it has to be catch(\Exception $e). In my case I was trying to connect to an IMAP server:

$mail = new \Zend\Mail\Storage\Imap(array(
    'host'     => 'imap.gmail.com',
    'user'     => 'USERNAME@gmail.com',
    'password' => 'PASSWORD',
    'ssl'      => 'SSL',
    'port'     => 993
));

With wrong login data this throws a runtime exception. I wanted a simple error handling done by myself so I added this:

try{
    $mail = new \Zend\Mail\Storage\Imap(array(
        'host'     => 'imap.gmail.com',
        'user'     => 'USERNAME@gmail.com',
        'password' => 'PASSWORD',
        'ssl'      => 'SSL',
        'port'     => 993
    ));
}catch(\Exception $e){
    die("wrong credentials!");
}

which makes it work in Zend Framework 2.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.