Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Assuming that we're trying to catch all the possible exceptions within try ... catch block when persisting an object into database.

Handling exceptions

namespace Football\FrontendBundle\Controller;

use Doctrine\DBAL\DBALException;
use Doctrine\ORM\ORMException;
use PDOException;
use Exception;
use Football\FrontendBundle\Entity\Country;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class CountryController extends Controller
public function createAction()
$country = new Country();
$country->setName('United Kingdom');

$em = $this->getDoctrine()->getEntityManager();

try {
$message = 'Done';
} catch (DBALException $e) {
$message = sprintf('DBALException [%i]: %s', $e->getCode(), $e->getMessage());
} catch (PDOException $e) {
$message = sprintf('PDOException [%i]: %s', $e->getCode(), $e->getMessage());
} catch (ORMException $e) {
$message = sprintf('ORMException [%i]: %s', $e->getCode(), $e->getMessage());
} catch (Exception $e) {
$message = sprintf('Exception [%i]: %s', $e->getCode(), $e->getMessage());

echo $message;