问题
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