Sub topic 1
What happens when Apex code fails
Prerequisites
This chapter assumes you can read basic Apex: classes, methods, variables, and control flow. You should know how to write a SOQL query and a DML statement. You should also know that Salesforce runs your code inside a transaction and enforces governor limits.
What an exception is
An exception is an object that Apex creates when code cannot continue normally. Examples are a query that returns no rows for a single-record variable, a DML statement that violates a validation rule, or a method call on a null reference.
When this happens, Apex stops running the current statement. It does not run the statements after it in that block. Instead, it looks for code that handles the problem.
How an exception propagates
Propagation is how an exception moves up the call stack. The call stack is the chain of methods that called each other to reach the current line.
- Apex looks for a matching
catchblock in the method where the exception occurred. - If none exists, the exception leaves that method and goes to the caller.
- Apex repeats the search in each caller, one level at a time.
- If no method on the stack handles it, the exception is unhandled.
What happens to the transaction
An unhandled exception ends the whole transaction, and Salesforce rolls back every database change made in it. Records you inserted or updated earlier in the same transaction are not saved. The user sees an error message, or the failure appears in the debug log for background work.
A caught exception is different. Once a catch block handles it, the transaction continues. Changes made before the failure stay in place unless you roll them back yourself.
The example below shows the difference. The first insert succeeds. The second throws an exception.
public class ExceptionDemo {
public static void run() {
insert new Account(Name = 'First');
insert new Account(); // Name is required, so this throws a DmlException
System.debug('This line never runs.');
}
}If you call ExceptionDemo.run() and nothing catches the DmlException, the account named First is rolled back along with everything else. If a caller wrapped the call in try and catch, First would stay in the transaction and be committed when it finishes.
Errors you can and cannot catch
Most runtime errors are exceptions that you can catch. These include QueryException, DmlException, and NullPointerException. A few failures cannot be caught. The main one is a governor limit exceeded error, such as too many SOQL queries. Apex ends the transaction immediately, and no catch block runs.
Why this matters
You decide what each failure should do. You can let it propagate so the whole transaction rolls back. You can catch it and recover. You can catch it, clean up, and report a clearer error. The next chapters cover the syntax for each choice.
Exercise
A method inserts two accounts. The first is valid. The second is missing the required Name field, and no code anywhere in the call stack catches the resulting exception. Is the first account saved? Explain.
Show answer
No. The unhandled DmlException ends the transaction, and Salesforce rolls back all database changes made in it, including the valid first insert.