Sub topic 3
What Happens When an Exception Is Not Handled
This chapter assumes you can write basic Apex, run DML statements such as insert and update, and use try/catch with DmlException. It also assumes you know that governor limits exist.
The rule: unhandled means rolled back
An Apex transaction is all the work done from one entry point, such as a trigger, a web service call, or an anonymous Apex run. If code in that transaction throws an exception and nothing catches it, Apex rolls back every change the transaction made. None of its DML is saved. Salesforce also reports the error to the user or caller, and it may write a debug log entry or send an unhandled-exception email.
The rollback covers all DML in the transaction, not only the statement that failed. A record inserted ten lines earlier is undone too.
public class UnhandledDemo {
public static void run() {
insert new Account(Name = 'First');
insert new Account(); // Name is required, so this throws a DmlException
insert new Account(Name = 'Never reached');
}
}If you call UnhandledDemo.run() from anonymous Apex, the second insert throws and nothing catches the exception. The transaction ends with an error, and the account named First is not saved either. The third line never runs.
What counts as unhandled
An exception is unhandled when it travels out of your outermost method without being caught. Common causes include:
- A
DmlExceptionfrom a failedinsert,update,delete, orupsert. - A
QueryException, for example when a single-row assignment returns zero or several rows. - A
NullPointerException. - A governor limit error. A
LimitExceptioncannot be caught, so it always ends the transaction and rolls it back. - A custom exception that you
throwand never catch.
// Anonymous Apex
insert new Account(Name = 'Before the query');
Account a = [SELECT Id FROM Account WHERE Name = 'No such name' LIMIT 1];
// The assignment throws a QueryException because no row matches.
// The first insert is rolled back.Seeing the rollback happen
The following diagram shows the flow from the entry point to the end of the transaction.
The "caught" branch is the subject of a later chapter. Here, remember only that an uncaught exception takes the right-hand path.
Practical effects
- Users see an error. In a Lightning page or Visualforce page, the user gets a message and no records change.
- Tests fail. An unhandled exception in a test method fails that test. The test framework rolls back the test's data as well.
- Partial work is never left behind. You do not have to write cleanup code for DML that already ran. The platform removes it for you.
This is useful because it keeps related changes consistent. If an order and its line items must exist together, an unhandled failure on the line items also removes the order.
public class OrderCreator {
public static void create() {
Account acct = new Account(Name = 'Order Customer');
insert acct;
Contact c = new Contact(AccountId = acct.Id); // LastName is required
insert c; // throws DmlException, so acct is rolled back too
}
}Common errors
- Assuming earlier DML survives. After an unhandled exception, it does not.
- Trying to catch a
LimitException. Acatch (Exception e)block does not catch it, so the transaction still rolls back. - Reading the
Idof a rolled-back record and expecting the row to exist. The row is gone.
Exercise
A method inserts an Account successfully, then runs a SOQL query that assigns zero rows to a single Account variable. Nothing catches the resulting exception. Is the Account saved?
Show answer
No. The query throws a QueryException that nobody catches, so the transaction ends with an error and Apex rolls back all of its DML, including the Account insert.
Exercise
Why can a catch (Exception e) block not save you from hitting the SOQL query limit in the middle of a transaction?
Show answer
The governor limit error is a LimitException, which cannot be caught. The transaction stops and all of its DML is rolled back.