From the article
try {
$db = new SQLite('/www/sitepoint/php5/sitepoint.sqlite');
try {
# Simulate bad connection
// throw new IOError('Lost connection to database');
$result = $db->query('SELECT * FROM users');
while ( $row = $result->fetch() ) {
echo ( '<pre>' );
print_r($row);
echo ( '</pre>' );
}
} catch ( SQLError $e ) {
echo ( 'There is something wrong with the query: '.$e->getMessage() );
} catch ( Exception $e ) {
// Re-throw exception
throw ($e);
}
} catch ( IOError $e ) {
echo ( 'Major problems!' );
error_log ( date('YmdHis').": ".$e->getMessage()."\n",3,'errors.txt' );
} catch ( Exception $e ) {
echo ( 'Hmmm - no idea how to deal with this: '.$e->getMessage() );
}
I'd be really dissapointed if that's how we had to handle the possibility of multiple errors being thrown. In Java you can have as many catch clauses per try as you like so this could be re-written as.
try {
$db = new SQLite('/www/sitepoint/php5/sitepoint.sqlite');
# Simulate bad connection
// throw new IOError('Lost connection to database');
$result = $db->query('SELECT * FROM users');
while ( $row = $result->fetch() ) {
echo ( '<pre>' );
print_r($row);
echo ( '</pre>' );
}
} catch ( SQLError $e ) {
echo ( 'There is something wrong with the query: '.$e->getMessage() );
} catch ( IOError $e ) {
echo ( 'Major problems!' );
error_log ( date('YmdHis').": ".$e->getMessage()."\n",3,'errors.txt' );
} catch ( Exception $e ) {
echo('Hmm - no idea how to deal with this: '.$e->getMessage());
}
which makes much more sense to me. You just start off with the more specific Exceptions and lead down the more general (ending with the most general, ie Exception).