Process Automation and Logic28% of the exam
Exception handling and custom exceptions
How try, catch and finally run in Apex, which built-in exceptions to expect and which can't be caught, how to declare and throw a custom exception, and how DML errors, addError and partial-success DML decide what is rolled back.
Implement exception handling in Apex, including custom exceptions as needed.
~7 min read · 9 practice questions ·
What the exam expects
- Predict which lines run in a try-catch-finally block, including the order in which several catch blocks are tested.
- Name the built-in exception a scenario raises (DmlException, QueryException, NullPointerException, ListException, SObjectException) and know that LimitException can't be caught.
- Declare, throw and catch a custom exception, and wrap a lower-level exception as its cause.
- Handle DML failures: catch a DmlException, use partial-success Database methods, or call
addError()in a trigger, and predict what is rolled back in each case.
Key terms: exception (an error that halts normal execution and bubbles up to the nearest matching catch); catch block (code that handles one exception type); finally block (cleanup code that always runs); custom exception (your own class that extends Exception); addError() (marks a trigger record as failed with a message).
Key facts
try requirements
- Detail
- At least one catch or a finally; at most one finally
Catch order
- Detail
- First matching catch runs; general Exception must be last
finally
- Detail
- Runs whether or not an exception was thrown, whatever its type
Uncatchable
- Detail
- LimitException, failed assertions, license exceptions; catch and finally don't run
Custom exception rules
- Detail
- Extends Exception; name ends with Exception
Custom constructors
- Detail
- Inherited: none, message, cause, or message plus cause
Zero rows into one sObject
- Detail
- QueryException "List has no rows for assignment to SObject"
DmlException detail
- Detail
getNumDml(),getDmlMessage(i),getDmlFieldNames(i)
Unhandled exception
- Detail
- DML already processed is rolled back; email sent
allOrNone default
- Detail
- true: the Database method behaves like the DML statement
addError from API bulk call
- Detail
- Bad records set aside; the rest saved
Sources for this section (6)
- Exception Statementsdeveloper.salesforce.com (opens in a new tab)
- Create Custom Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Built-In Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Exceptions in Apexdeveloper.salesforce.com (opens in a new tab)
- Manipulate Records with DMLtrailhead.salesforce.com (opens in a new tab)
- Trigger Exceptionsdeveloper.salesforce.com (opens in a new tab)
- Why must a catch for DmlException come before a catch for Exception?
- When does a finally block not run?
- Which two rules make a class a valid custom exception?
- Why does addError in a trigger fired by a bulk API load still let valid records save?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 21 lessons of the Platform Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (9 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams