Apex ships with a long list of built-in exception types, all rooted in System.Exception. The ones you must recognise in an interview are the everyday ones — DmlException, QueryException, NullPointerException, ListException, SObjectException, LimitException, CalloutException, JSONException.
The everyday eight
| Exception | When it fires | Typical fix |
|---|---|---|
DmlException | Insert / update / delete / upsert failed validation, sharing, or required field | Inspect getDmlFieldNames(), getDmlStatusCode() |
QueryException | SOQL returned 0 rows or more than 1 to a single-row sink, or query was malformed | Use LIMIT 1 plus list assignment, or wrap in try/catch |
NullPointerException | Dereferenced a null variable | Null check before use |
ListException | Out-of-bounds index, duplicates in a Set cast, or a List<T> operation that can’t apply | Check size() before indexing |
SObjectException | Accessed a field that wasn’t queried, or a field that doesn’t exist on the SObject | Add the field to the SOQL SELECT list |
LimitException | Hit a governor limit (CPU, heap, query rows, DML rows, callouts…) | Refactor — cannot be caught |
CalloutException | HTTP callout failed, timed out, or hit named-credential issues | Inspect status, retry policy |
JSONException | JSON.deserialize() could not map the input to your type | Validate JSON shape, use untyped parser |
A larger reference list
There are dozens more — these come up in specific scenarios:
EmailException— Messaging.SingleEmailMessage send failureMathException— divide by zero, overflowStringException— invalid string operationTypeException— invalid cast orType.forNamefailureXmlException— XML parsing errorAsyncException— async job submission problemSecurityException—Security.stripInaccessiblefield/object access denialNoAccessException— record-level access deniedNoDataFoundException— base type thatQueryExceptionextends for empty-result scenariosVisualforceException— Visualforce controller issueSearchException— SOSL failureInvalidParameterValueException— bad parameter to a system methodRequiredFeatureMissingException— feature not enabled in org
Quick demos
// DmlException
try {
insert new Contact(); // missing required LastName
} catch (DmlException e) {
System.debug(e.getDmlMessage(0)); // Required fields are missing: [LastName]
}
// QueryException — too many rows for a single SObject assignment
try {
Account a = [SELECT Id FROM Account]; // org has more than one
} catch (QueryException e) {
System.debug(e.getMessage()); // List has more than 1 row for assignment to SObject
}
// NullPointerException
try {
Account a;
String n = a.Name;
} catch (NullPointerException e) {
System.debug('Account was null');
}
// SObjectException — field not queried
try {
Account a = [SELECT Id FROM Account LIMIT 1];
System.debug(a.Industry); // not in SELECT
} catch (SObjectException e) {
System.debug(e.getMessage());
}
Custom exception types extend Exception
public class ValidationException extends Exception {}
public class IntegrationException extends Exception {}
You can catch them with catch (ValidationException e), which is much cleaner than parsing strings out of a generic Exception.
Common follow-ups
- Catching
LimitException? — Not catchable in user code. The runtime owns it. - DmlException details? —
getNumDml(),getDmlMessage(i),getDmlFieldNames(i),getDmlId(i),getDmlStatusCode(i)— give per-row failure info. - Difference between
NullPointerExceptionandSObjectException? — Null pointer is dereferencing a null variable. SObject exception is asking an SObject for a field the SOQL didn’t include.
Verified against: Apex Developer Guide — Built-In Exceptions. Last reviewed 2026-05-17 for Spring ‘26.