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
Student student;— declared but never assigned an object — its value isnull.System.debug(student.getName());— calls a method on something that'snull— 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
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
List<String> names = new List<String>{'Amara', 'Ben'};— a list with only two elements, at indexes0and1.System.debug(names[5]);— tries to access index5, 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());
}
try {— the risky block.Decimal result = 10 / 0;— throws aMathException.} catch (MathException e) {— catching the specific exception type first lets you react precisely to the failure you actually expect.System.debug('Specifically a math problem: ' + e.getMessage());— the precise handling.} catch (Exception e) {— a trailing generic catch acts as a catch-all safety net for anything else.System.debug('Some other problem: ' + e.getMessage());— the generic handling.}— 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.
Common Built-In Exceptions Quiz
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.