2

I am using the ImapMail Library and try to initiate a new Mailbox:

    try{
        $mailbox = new ImapMailbox($url, $username, $password);
    }catch (\Exception $e){
        return new Response("fail");
    }

But the above try/catch does not work, although symfony gives me this:

Connection error: (is empty) 
 500 Internal Server Error - Exception 

the Stacktrace

 in vendor/php-imap/php-imap/src/PhpImap/Mailbox.php at line 67:

    protected function initImapStream() {
        $imapStream = @imap_open($this->imapPath, $this->imapLogin, $this->imapPassword, $this->imapOptions, $this->imapRetriesNum, $this->imapParams);
        if(!$imapStream) {
            throw new Exception('Connection error: ' . imap_last_error());
        }
        return $imapStream;
    }

(Direct link to function on github)

It does throw a new Exception, why can't I catch it?

Any hint appreciated!

7
  • typo in catch (\Exception $e) ?? was it meant to be catch (Exception $e) Commented Oct 29, 2015 at 10:44
  • Well, I use the global namespace Exception, but even if I don't no catch... Commented Oct 29, 2015 at 10:48
  • Is this the entire code or do you have other catch blocks on the same level? Perhaps the error is caught by another catch block? Commented Oct 29, 2015 at 10:56
  • No, here is the whole code: pastebin.com/GMTXyAwm. The class is on github (link above) Commented Oct 29, 2015 at 10:57
  • have you added the use statement for the Response object? Commented Oct 29, 2015 at 11:09

1 Answer 1

2

The reason is very simple. You call only class constructor in try block. If you look into the class source code, you will see that constructor does call nothing.

https://github.com/barbushin/php-imap/blob/master/src/PhpImap/Mailbox.php#L20-L31

It means code throwing exception must be under you try/catch. You have to move it inside try block if you want to catch the exception. I am talking about $mailbox->checkMailbox(). This command throws exception.

Sign up to request clarification or add additional context in comments.

1 Comment

PERFECT, I tried it with the whole IF Statement of checkMailbox(), no result. Now I know why...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.