Documentation Level: 
Documentation Status: 
No known problems

The Database API throws exceptions on error, which can be picked up by wrapping your database operations in try {} catch() {} blocks, as shown in this example:

// The transaction opens here.
$txn = db_transaction();
try {
$id = db_insert('example')
'field1' => 'mystring',
'field2' => 5,
return $id;
catch (Exception $e) {
// Something went wrong somewhere, so roll back now.
// Log the exception to watchdog.
watchdog_exception('type', $e);