Sub topic 3
Common built-in exception types
This chapter assumes you can write basic Apex classes and methods, run SOQL queries and DML statements, and know that governor limits exist. It also assumes you have seen a try/catch block. Here you will learn which built-in exceptions show up most often, what causes each one, and how to catch the right one.
Every exception is a class
Apex exceptions are classes. All of them extend the base Exception class, so each one has the same methods, such as getMessage(). The built-in types are named for the kind of failure they report. If you catch a specific type, your handler runs only for that failure.
The types you will meet most
| Exception | Typical cause |
|---|---|
| `DmlException` | An `insert`, `update`, `delete` or `upsert` fails, for example a required field is missing. |
| `QueryException` | A SOQL query assigned to a single record returns no rows or more than one row. |
| `NullPointerException` | You call a method or read a field on a variable that is `null`. |
| `ListException` | You read a list index that does not exist. |
| `MathException` | You divide by zero. |
| `TypeException` | A value cannot be converted to the type you asked for, such as `Integer.valueOf('abc')`. |
| `SObjectException` | You read a field that the query did not select, or set a field that cannot be set. |
| `LimitException` | A governor limit is exceeded. You cannot catch this one. |
The last row is special. A governor limit failure ends the transaction no matter what your catch blocks say. A later chapter covers this in detail.
Seeing them happen
Each snippet below triggers one exception and catches it by its specific type.
try {
Account a = [SELECT Id FROM Account WHERE Name = 'Does Not Exist' LIMIT 1];
System.debug(a.Id);
} catch (QueryException e) {
System.debug('Query failed: ' + e.getMessage());
}The query returns zero rows. Assigning an empty result to one Account variable throws QueryException.
try {
insert new Account();
} catch (DmlException e) {
System.debug('DML failed: ' + e.getMessage());
}Name is required on Account, so the insert fails with DmlException.
List<Integer> numbers = new List<Integer>{ 1, 2, 3 };
try {
System.debug(numbers[5]);
} catch (ListException e) {
System.debug('Bad index: ' + e.getMessage());
}
try {
Integer result = 10 / 0;
} catch (MathException e) {
System.debug('Math failed: ' + e.getMessage());
}
try {
Account a = [SELECT Id FROM Account LIMIT 1];
System.debug(a.Name);
} catch (SObjectException e) {
System.debug('Field not queried: ' + e.getMessage());
}The third example needs at least one Account in the org. If there is none, the query throws QueryException first, which this block does not catch. This shows why catching the exact type matters.
Choosing what to catch
Catch the narrowest type that matches the failure you expect, and handle it in a way that makes sense for that failure. A missing record might mean you use a default value. A DML failure might mean you report which field was wrong. A NullPointerException usually means a bug, so fix the cause instead of catching it.
Exercise
A method runs Contact c = [SELECT Id FROM Contact WHERE Email = '[email protected]']; and the org has two contacts with that email. Which built-in exception is thrown, and how can you avoid it without a try block?
Show answer
A QueryException is thrown, because a single-record assignment needs exactly one row. To avoid it, assign the result to a List<Contact> and check its size before using the first element.