6

I'm trying to catch a PDOException in laravel 3 but it seems as if I cannot do this. My code is as follows:

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()));
}

The general exception is caught if the other parts in the 'try' throw an exception. If they do not, everything runs clean. If the create has an issue executing the MYSQL statement it does not throw a PDOException, it is only throwing a general Exception.

WildBill
  • 9,143
  • 15
  • 63
  • 87
  • why is there a \ in front of your `PDOException` – cmorrissey May 07 '14 at 19:43
  • I've read you can place a '\' in front of PDOException for namespace issues. Even if I remove the '\' I still am unable to catch a PDOException. I should note that the error on the create is being thrown as a general exception... – WildBill May 07 '14 at 19:45
  • 4
    Why not dump out the type of `$e`, eg `get_class($e)`? That will at least tell you the type of exception being caught in the general exception handler – Phil May 21 '14 at 02:15
  • 1
    Please expand `Myobject` which seems to be relevant (you expect it to throw a PDOException!) so that people can easily reproduce the issue. – menjaraz May 25 '14 at 11:30
  • Please remove the first catch block and var_dump the generic Exception that you are supposed to get when PDOException is thrown... – Feras May 26 '14 at 20:00
  • Without knowing what the DB class is `throw`ing, there's no way to answer this. – miken32 Mar 17 '17 at 23:55

1 Answers1

-1

A model wouldn't actually throw PDOException, that would be caught internally and a \Illuminate\Database\QueryException would be thrown instead, try catching that.

ollieread
  • 6,018
  • 1
  • 20
  • 36
  • That is not being caught either. To be clear, I simply replaced `\PDOException` with `QueryException` in the above code and the regular `Exception` is all that is being caught. – WildBill May 08 '14 at 12:50
  • If your code is namespaced you'll need to provide the fully qualified namespace, otherwise it'll never be caught. – ollieread May 08 '14 at 18:20
  • My code is not name spaced, I was merely copying the example which showed the example. I only have one instance of that class/method. Thus, the question still remains... – WildBill May 21 '14 at 02:10
  • 1
    Wouldn't it be `catch (\Illuminate\Database\QueryException $queryException)` in the global namespace (which OP is apparently in)? That being said, `QueryException` extends `PDOException` anyway – Phil May 21 '14 at 02:16
  • Yeah, either way, I'm pretty sure it needs to be the full namespace, exceptions are a bit funny tbh. @WildBill just for arguments sake, try removing the `Exception` catch. – ollieread May 22 '14 at 08:34
  • 1
    `Illuminate` has nothing to do with Laravel 3! – menjaraz May 25 '14 at 11:00