【发布时间】:2015-12-10 17:29:32
【问题描述】:
我尝试在 Cakephp v3.0 中捕获异常,但它似乎不起作用:
try{
$email = new Email('default');
$email->from([Configure::read('email') => Configure::read('emailName')])
->to(Configure::read('email'))
->bcc($to)
->subject(__('XXXX') . ' : ' . __('XXXX'))
->template('fail', 'default')
->emailFormat('html')
->send();
} catch (Exception $ex) {
}
它没有捕捉到异常:
Could not send email: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() Cake\Network\Exception\SocketException
很烦人,我用它来捕捉本地服务器上发送失败的电子邮件。
非常感谢。
【问题讨论】:
-
您指向一个导入的类,或者指向与当前类在同一命名空间中的一个类,因此请检查您的代码中的
Exception实际指的是什么 - 如果我猜的话,我会说它可能不是全局命名空间中的本机\Exception类。 ps,请务必在发布错误时包含堆栈跟踪和上下文信息! -
使用 RuntimeException 而不是 Exception,成功了。谢谢!
-
@Gael.D 我一直在为这个问题而苦恼。使用您的解决方案。没有任何输出。
标签: php cakephp exception cakephp-3.0