Zend Framework 2 - catching an exception


November 25, 2014 at 19:41
Zend Framework

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.


Tags: Errorhandling Exception IMAP Zend Framework 2


Hinterlasse einen Kommentar:
Search