Should prepare and bindParam statements be put in the try{} block when trying to catch exceptions. Can prepare and bindParam cause/generate/whatever-the-proper-term-is exceptions?
Right now I’m putting only execute() in the try{}, I don’t know if that’s the proper way of doing things.
So, should I do:
$s = $dbh->prepare("select * from products where id=:p_id");
$s->bindParam(":p_id",$p_id,PDO::PARAM_INT);
try {
$s->execute();
} catch (PDOException $e) {
log_error("MySQL error: ".$e->getMessage());
}
or
try {
$s = $dbh->prepare("select * from products where id=:p_id");
$s->bindParam(":p_id",$p_id,PDO::PARAM_INT);
$s->execute();
} catch (PDOException $e) {
log_error("MySQL error: ".$e->getMessage());
}
Normally it shouldn’t be used in the application code at all.
Many sketch codes that uses
try.. catchare just sketches, to show some idea. And shouldn’t be copied as is.There should be an application-wide exception handler which is responsible for catching all the exceptions and take appropriate action (log the error message and throw 503 HTTP error normally).