Page 1 of 1

Question regarding exceptions

Posted: Thu Jul 29, 2010 5:20 am
by social_experiment
If you have the following code as part of a function that creates the connection to a database server :

Code: Select all

<?php
if (!$connect) throw new Exception('Cannot connect');
else {
 if (!$select_db) throw new Exception('Cannot select database');
}	
?>
and the following code as part of a function that processeses a mysql query :

Code: Select all

<?php
if (!$sql) throw new Exception('Error with query');
?>
would the following code sample, to catch the exceptions; if any, be correct?

Code: Select all

<?php
try {
 //code that executes query
}
catch (Exception $e) {
 echo $e->getMessage();
} ?>
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)?

Re: Question regarding exceptions

Posted: Thu Jul 29, 2010 1:06 pm
by Jade