CakePHP 3 - 捕获错误 [英] CakePHP 3 - Catch Error
本文介绍了CakePHP 3 - 捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
我得到的是一个PDO异常。没有冲突的那些仍然插入,我想要什么。
What I get is an PDO exception. The ones that don't conflict are still inserted, what I want.
所以我只想捕获PDO异常,但是如何?
So I only want to catch the PDO exception, but how?
推荐答案
如果您只想捕获特定的异常,请在catch块中指定异常类。
If you only want to catch a specific exception, specify the exception class in the catch block.
try
{}
catch (\PDOException $e)
{}
这篇关于CakePHP 3 - 捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文