我试图在 laravel 3 中捕获 PDOException,但似乎我无法做到这一点。我的代码如下:
try{
DB::connection()->pdo->beginTransaction();
Myobject::create($cleaned_input_array);
// do other stuff that could possibly throw a custom exception
DB::connection()->pdo->commit();
}
catch(\PDOException $e)
{
DB::connection()->pdo->rollBack();
return HTTP_STATUS::response(BAD_REQUEST, array("error creating");
}
catch(Exception $e)
{
DB::connection()->pdo->rollBack();
return HTTP_STATUS::response(BAD_REQUEST, array($e->getMessage()));
}
如果“try”中的其他部分抛出异常,则捕获一般异常。如果他们不这样做,一切都会干净利落。如果 create
在执行 MYSQL 语句时出现问题,它不会抛出 PDOException,它只会抛出一般异常。
最佳答案
模型实际上不会抛出 PDOException
,它会在内部被捕获,而是会抛出 \Illuminate\Database\QueryException
, try catch 它。
关于php - 无法在 laravel 3 中捕获 PDOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23526897/