Intermediate 25 min read

Common Built-In Exceptions

By the end of this lesson, you'll be able to:

  • Recognize NullPointerException, MathException, and ListException by name and cause
  • Predict which built-in exception a given piece of code would throw
  • Catch a specific exception type rather than a generic one when possible

Prerequisites: "Try, Catch, and Finally"

NullPointerException

Student student;
System.debug(student.getName()); // NullPointerException
  1. Student student; — declared but never assigned an object — its value is null.
  2. System.debug(student.getName()); — calls a method on something that's null — there's no actual object there to act on.

Warning: Line 2 throws an uncaught NullPointerException, immediately stopping execution. This is the single most common runtime exception in Apex, and the null-check guards from Module 12's "Handle Errors Gracefully" lesson exist specifically to prevent it.

MathException

Decimal result = 10 / 0; // MathException
  1. Decimal result = 10 / 0; — invalid arithmetic — division by zero being the classic case, first seen back in Module 8's calculator project.

Warning: This line throws an uncaught MathException and halts execution immediately, exactly like the NullPointerException above.

ListException

List<String> names = new List<String>{'Amara', 'Ben'};
System.debug(names[5]); // ListException: index out of bounds
  1. List<String> names = new List<String>{'Amara', 'Ben'}; — a list with only two elements, at indexes 0 and 1.
  2. System.debug(names[5]); — tries to access index 5, which doesn't exist.

Warning: Line 2 throws an uncaught ListException: index out of bounds, halting execution immediately.

Catching a specific type vs a generic one

try {
    Decimal result = 10 / 0;
} catch (MathException e) {
    System.debug('Specifically a math problem: ' + e.getMessage());
} catch (Exception e) {
    System.debug('Some other problem: ' + e.getMessage());
}
  1. try { — the risky block.
  2. Decimal result = 10 / 0; — throws a MathException.
  3. } catch (MathException e) { — catching the specific exception type first lets you react precisely to the failure you actually expect.
  4. System.debug('Specifically a math problem: ' + e.getMessage()); — the precise handling.
  5. } catch (Exception e) { — a trailing generic catch acts as a catch-all safety net for anything else.
  6. System.debug('Some other problem: ' + e.getMessage()); — the generic handling.
  7. } — closes the chain.

Apex checks catch blocks top to bottom, matching the first one whose type fits — the same ordering logic as an if/else if chain from Module 6.

Exercise

As a comment, name which built-in exception type each of these would throw: (1) calling a method on a null variable, (2) accessing List index 10 on a 3-item list.

Show hint

NullPointerException and ListException.

APEX

Common Built-In Exceptions Quiz

1. What causes a NullPointerException?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Apex ships with several built-in exception types for common failure modes — recognizing them by name makes debug logs far faster to read.