[英]Catch Exception in Cakephp 3 : not working
我试图在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
或更具体的命名空间的异常名称
try {
// code
} catch (\Exception $e) {
// error
}
当我试图捕捉MissingConnectionException
时,我遇到了类似的问题。
在我的情况下,以下行解决了我的问题。
use Cake\Core\Exception\Exception;
...
try {
// Your test code here
} catch (Exception $e) {
...
}
希望对你有所帮助。
您可以尝试使用try
- catch
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 (\PDOException $e) {
$error = $e->getMessage();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.