Do I need to use the try and catch block to display errors with PHP\'s PDO extension?
For example, with mysql you can usally do something like:
if ( ! $q
PDO by default supports it's own Exception Class, PDOException
.
There should be no reason why you cannot use:
try{
$query = $db->prepare(...);
if( !$query ){
throw new Exception('Query Failed');
}
}
catch(Exception $e){
echo $e->getMessage();
}
The reason I only catch Exception
is because PDOException
is a child of Exception. Catching the Parent will catch ALL children.