In our code this week we have added a try/catch/throw for exceptions generated when connecting to a database. This is gr

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

In our code this week we have added a try/catch/throw for exceptions generated when connecting to a database. This is gr

Post by answerhappygod »

In our code this week we have added a try/catch/throw forexceptions generated when connecting to a database. This isgreat. The question now is, how far should this be extended?Look through the rest of the code that we have in place. Towhat extent can we capture errors and throw exceptions? Should wecreate an exception class for every type of error? A classhierarchy of errors? Or should we have one catch all exception fordatabases errors? Or do nothing at all?
For example, look at the code below. All of the APIs can returnerrors. Prepare, execute, bind_results. How defensiveshould our code get? In the event of errors in these fragments whatshould we do? Obviously, we are not doing anything right now ...should we do something and if so how should we structure our codeto handle any detected errors?
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($bname, $summary);
//echo "<br/>Blogs Summary</p>";
while($stmt->fetch()) {
echo $bname." ".$summary."</p>";
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply