Symfony2 Controller won't catch exception
Try to change Exception
→ \Exception
if you didn't specified PDOException
as Exception
in a use statement. PHP tries to find \YourNamespaceWithController\Exception
instead of \Exception
(and it does not check the existence of such exception).
It is better to catch the exception you really want to catch. In this example that is probably Doctrine/DBAL/DBALException and/or Doctrine/DBA/DBAException.
Thus
catch (Doctrine\DBAL\DBALException $e) {
$result = -1;
};
I would recomment doing something like:
} catch (\Exception $e) {
switch (get_class($e)) {
case 'Doctrine\DBAL\DBALException':
echo "DBAL Exception<br />";
break;
case 'Doctrine\DBA\DBAException':
echo "DBA Exception<br />";
break;
default:
throw $e;
break;
}
}
This actually catches the DB exceptions, and if for some reason some other exception occures, this is rethrown back into Symfony2.