Catch Exception in Cakephp 3 : not working

蓝咒 提交于 2019-12-03 11:58:30
José Lorenzo Rodríguez

Adding the answer here, just to bring dow the unanswered questions stats down:

You need to use \Exception or a more specific, namespaced, exception name

try {
    // code
} catch (\Exception $e) {
    // error
}

I had a similar issue when I tried to catch MissingConnectionException.

In my case, following lines solved my problem.

use Cake\Core\Exception\Exception;
...
try {
    // Your test code here
} catch (Exception $e) {
    ...
}

Hope it would be help for you.

You could try using 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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!