Try\catch around PDO code - what code to include?

When I execute a query with PDO, I put the query in a try catch block to catch PDOExceptions like this:

try
{
			
// sql, binding, etc
$stmt->execute();
			
}
catch (PDOException $e)
{
}

My question is whether I should have the code below (which follows the code above this paragraph) also in a try/catch block (and why), and if I should be looking for PDOExceptions, or assuming that the query executed successfully, if there exists any reason to try to catch PDOExceptions. (I have not had an error generated yet.)

$db_return = $stmt->fetch();

$count =  $stmt->rowCount();

Out of curiosity, when you do indeed catch a PDO exception, what do you put in the catch block to handle that error? Because if the catch block is left empty, then you’re effectively just ignoring the error.

I would think neither block of code should have a try/catch at this level. If your database craps out on you, there’s not much you can do about it. You should probably allow the exception to bubble to the top of your application. That’s where you’ll catch it and render a “Something went wrong” page for the user, and optionally also send an email to yourself so that you’re aware when your site suffers a critical problem.

Did you configure PDO to throw Exception in case of an error?

If you want to catch errors,enable the setAttribute.

try {
        $con = new Connection();
        $db = $con->dbconnection();
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


        $cmd = $db->prepare("INSERT into mytable (column1,column2,column3) values(?,?,?)");
        $cmd->execute(array($param1,$param2,$param3));
      

        $db = null;
        echo "successfully";

    } catch (PDOException $ex) {
        echo  $ex->getMessage();
    }

Hope this helps.

Hi all,

I just left my exception handling code out. I should have put a comment there. Sorry if this mislead you!

@Jeff_Mott: I do let PDOExceptions bubble up to stop the application in certain critical areas, but in other areas I do not because it isn’t critical to the purpose of a page. In the cases where it isn’t critical, I still want to catch all of the exceptions and handle them.

@Dormilich/ @Jemz: I have PDO already set to give exceptions, but thanks for notice.

To re-do my OP:

Should I do this:

try
{

  // sql, binding, etc
  $stmt->execute();

}
catch (PDOException $e)
{
 // exception handling
}
// additional post $stmt->execute() code
$db_return = $stmt->fetch();

$count =  $stmt->rowCount();

Or this:

try
{

  // sql, binding, etc
  $stmt->execute();

  // additional post $stmt->execute() code
  $db_return = $stmt->fetch();

  $count =  $stmt->rowCount();

}
catch (PDOException $e)
{
 // exception handling
}

I have been using the former method, but I realized the " // additional post $stmt->execute() code" is accessing PDO features, so perhaps it should also be in the try/catch block? But if the code in the try / catch block does not give an error, it hard for me to see how the " // additional post $stmt->execute() code" could possibly give some new PDOException since the $stmt->execute() completed successfully, without exceptions.

I guess best practice would dictate to keep all PDO-related actions in a try clause since exceptions may be raised on any PDO method call. For example, if a connection to the MySQL server is lost between PDO action calls or the MySQL server dies, then they’ll cause exceptions to be thrown. The likelihood of either of those occurring is extremely slim, but they still could happen. So I tend to err on the side of caution and simply encapsulate all my PDO-related actions in a try…catch clause.