Can a batch process be stopped
Inside your batch callback, where you do the batch work, you should be able to set $context['finished'] to 1, and that will signal to Drupal that the batch is complete.

So, in your case:

$error = FALSE;
try {
  $sth = $dbh-&gt;prepare($sql);
  $sth-&gt;bindParam(':newid',$new_id,PDO::PARAM_INT);
  $sth-&gt;bindParam(':id',$id,PDO::PARAM_INT);
  if($sth-&gt;execute()) {
    $context['results'][] = $sth-&gt;rowCount();
  }
  else {
    $error = TRUE;
  }
}
catch (PDOException $e) {
  $error = TRUE;
}
// If there was an error, immediately complete the batch process.
if ($error) {
  $context['finished'] = 1;
}


And, even better, you can pass in an error message in the $context as well, which you can check for in your finished callback, then display to the end user in case of failure.

If you want to immediately stop processing anything in other callbacks in the $operations list, you might also need to check for a static variable at the beginning of each callback, and skip the callback if it's in an error condition. You could try using variable_get/variable_set, or some other mechanism that persists between page requests. This would definitely be a hackish/last-resort solution, though!

See also:


How does the batch API work internally?
Batch operations API documentation