Skip to main content

SF-0319 · Concept · Medium

Name a few built-in exception types?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

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

ExceptionWhen it firesTypical fix
DmlExceptionInsert / update / delete / upsert failed validation, sharing, or required fieldInspect getDmlFieldNames(), getDmlStatusCode()
QueryExceptionSOQL returned 0 rows or more than 1 to a single-row sink, or query was malformedUse LIMIT 1 plus list assignment, or wrap in try/catch
NullPointerExceptionDereferenced a null variableNull check before use
ListExceptionOut-of-bounds index, duplicates in a Set cast, or a List<T> operation that can’t applyCheck size() before indexing
SObjectExceptionAccessed a field that wasn’t queried, or a field that doesn’t exist on the SObjectAdd the field to the SOQL SELECT list
LimitExceptionHit a governor limit (CPU, heap, query rows, DML rows, callouts…)Refactor — cannot be caught
CalloutExceptionHTTP callout failed, timed out, or hit named-credential issuesInspect status, retry policy
JSONExceptionJSON.deserialize() could not map the input to your typeValidate JSON shape, use untyped parser

A larger reference list

There are dozens more — these come up in specific scenarios:

  • EmailException — Messaging.SingleEmailMessage send failure
  • MathException — divide by zero, overflow
  • StringException — invalid string operation
  • TypeException — invalid cast or Type.forName failure
  • XmlException — XML parsing error
  • AsyncException — async job submission problem
  • SecurityExceptionSecurity.stripInaccessible field/object access denial
  • NoAccessException — record-level access denied
  • NoDataFoundException — base type that QueryException extends for empty-result scenarios
  • VisualforceException — Visualforce controller issue
  • SearchExceptionSOSL failure
  • InvalidParameterValueException — bad parameter to a system method
  • RequiredFeatureMissingException — 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 NullPointerException and SObjectException? — 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.