Sub topic 7
Catching an exception does not undo earlier DML
This chapter assumes you can write basic Apex, run DML statements such as insert, and use try/catch with DmlException. It also assumes you know that Apex enforces governor limits. The previous chapter covered what happens when an exception is not handled. Here we look at what changes when you catch it.
The working model
An unhandled exception ends the transaction, and Apex rolls back all of its DML. A caught exception does not end the transaction. The catch block stops the error from propagating, so execution continues and the transaction later commits.
The catch block does not undo anything. Only the failing statement is affected. Earlier statements that succeeded stay pending in the transaction.
insert new Account(Name = 'Kept');
try {
insert new Account(); // Name is required, so this throws a DmlException
} catch (DmlException e) {
System.debug(e.getMessage());
}
// The exception was handled, so the first Account is still committed when the transaction ends.The second insert saves nothing, because a single DML statement is all-or-none. The account named Kept is committed when the transaction finishes.
Common errors
- Assuming
catchcleans up. Developers often write acatchblock that logs the error and expects the whole operation to be undone. It is not undone. - Leaving half-finished data. If you insert a parent record and its child insert fails, catching the error leaves a parent with no child.
- Swallowing errors silently. A
catchblock that does nothing hides the failure while the earlier work still commits.
Undoing the earlier work on purpose
To make a caught exception undo earlier DML, set a savepoint before the related work and roll back to it in the catch block. Savepoints are covered in detail in the next chapters.
Savepoint sp = Database.setSavepoint();
try {
Account acct = new Account(Name = 'Acme Test');
insert acct;
Contact c = new Contact(LastName = 'Doe', AccountId = acct.Id);
insert c;
} catch (DmlException e) {
Database.rollback(sp);
System.debug('Rolled back: ' + e.getMessage());
}If either insert fails, Database.rollback(sp) removes both. Another option is to rethrow the exception, or throw a new one, from the catch block. An exception that leaves your code unhandled rolls back the whole transaction.
Choosing between the options
| Goal | Approach |
|---|---|
| Fail the whole request on any error | Let the exception propagate |
| Report an error but keep earlier work | Catch and continue |
| Report an error and undo related work | Catch, then `Database.rollback(sp)` |
Exercise
A method runs insert new Account(Name = 'First');, then wraps insert new Contact(); (missing the required LastName) in a try/catch that only logs the error. The method returns normally. Is the account saved when the transaction ends, and how would you change the code so it is not?
Show answer
Yes, the account is saved. The caught DmlException does not end the transaction, and the failed contact insert saved nothing. To undo the account, call Database.setSavepoint() before the first insert and Database.rollback(sp) in the catch block. Rethrowing the exception from the catch block also works, because an unhandled exception rolls back the transaction.