我正在使用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;
}
( Direct link to function on github )
它确实抛出了一个新的异常,为什么我不能捕获它?
任何提示表示赞赏!
最佳答案
原因很简单。您仅在 try block 中调用类构造函数。如果您查看类源代码,您会发现构造函数没有调用任何内容。
https://github.com/barbushin/php-imap/blob/master/src/PhpImap/Mailbox.php#L20-L31
这意味着抛出异常的代码必须在您的 try/catch 下。如果你想捕获异常,你必须将它移动到 try block 内。我说的是$mailbox->checkMailbox()
。该命令抛出异常。
关于php - try{...}catch 在 PHP/Symfony2/PHPImap 中不会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412004/