Laravel – can’t catch PDO exceptions
我有这个:
1
2 |
$this->db = DB::connection()->getPdo();
$this->db->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_EXCEPTION); |
还有一个 try…catch 块,它似乎不做任何捕获:
1
2 3 4 5 6 7 |
try {
$this->SQL->execute($this->values); $this->insert_count++; } catch (PDOexception $e) { $this->errors[] = ‘PDO error: ‘ . $e->getmessage(); $this->error_count++; } |
…因为脚本仍然在 PDO 异常上退出:
[PDOException] SQLSTATE[23000]: Integrity constraint violation:
1062 Duplicate entry ’14’ for key ‘laptops_asset_tag_unique’
那么我该如何捕捉/处理 PDO 错误呢?
你有没有为你的异常命名空间?
您必须将 PDOException 导入您的类才能使用它。
在 php 文件的顶部添加以下语句
1
|
use PDOException;
|
- 这是另一个将引发的错误:CLASS NOT FOUND ‘PDOException’ …(如果存在)
来源:https://www.codenong.com/41074604/