CakePHP 3 - Catch Error

孤街醉人 提交于 2019-12-10 17:12:38

问题


use Cake\Core\Exception\Exception;


for($i=1; $i<count($values); $i++) {
        $entity = $table->newEntity();

        // irrelevant code

        try {
            $table->save($entity);
        } catch (Exception $e) {
            $errors[$i-1] = $values[$i];
        } finally {
            if(count($errors) == 0)
                $this->Flash->success('All rows are successfully imported. ');
            else {
                $this->Flash->error('Not all rows are successfully imported. ');
                debug($errors);
            }
        }
    }

What I want to do is catch the conflicted entities and show these to the user.

What I get is an PDO exception. The ones that don't conflict are still inserted, what I want.

So I only want to catch the PDO exception, but how?


回答1:


If you only want to catch a specific exception, specify the exception class in the catch block.

try
{}
catch (\PDOException $e)
{}


来源:https://stackoverflow.com/questions/35311368/cakephp-3-catch-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!