[英]try{…}catch does not throw exception in PHP/Symfony2/PHPImap
我正在使用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
堆栈跟踪
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;
}
( 直接链接到github上的函数 )
它确实引发了新的异常,为什么我不能捕获它?
任何提示表示赞赏!
原因很简单。 您只能在try块中调用类构造函数。 如果查看类源代码,您将看到构造函数没有执行任何操作。
https://github.com/barbushin/php-imap/blob/master/src/PhpImap/Mailbox.php#L20-L31
这意味着代码抛出异常必须在您的try / catch下。 如果要捕获异常,则必须将其移动到try块中。 我说的是$mailbox->checkMailbox()
。 此命令引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.