我正在使用ImapMail库并尝试启动新的邮箱:
try{
$mailbox = new ImapMailbox($url, $username, $password);
}catch (\Exception $e){
return new Response("fail");
}
但是上面的try / catch不起作用,虽然symfony给了我这个:
Connection error: (is empty)
500 Internal Server Error - Exception
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)
它确实抛出一个新的例外,为什么我无法抓住它?
任何暗示都赞赏!
答案 0 :(得分:2)
原因很简单。在try块中只调用类构造函数。如果你查看类源代码,你会发现构造函数没有调用任何内容。
https://github.com/barbushin/php-imap/blob/master/src/PhpImap/Mailbox.php#L20-L31
这意味着代码抛出异常必须在try / catch下。如果要捕获异常,则必须在try块内移动它。我在谈论$mailbox->checkMailbox()
。此命令抛出异常。