Question regarding exceptions
Posted: Thu Jul 29, 2010 5:20 am
If you have the following code as part of a function that creates the connection to a database server :
and the following code as part of a function that processeses a mysql query :
would the following code sample, to catch the exceptions; if any, be correct?
If there are 2 or 3 exceptions that can be caught, will the code above be correct or do i have to try and catch another exception (inside the first try statement)?
Code: Select all
<?php
if (!$connect) throw new Exception('Cannot connect');
else {
if (!$select_db) throw new Exception('Cannot select database');
}
?>Code: Select all
<?php
if (!$sql) throw new Exception('Error with query');
?>Code: Select all
<?php
try {
//code that executes query
}
catch (Exception $e) {
echo $e->getMessage();
} ?>