Sub topic 4
Reading exception details
This chapter assumes you can write a basic try/catch block and know the built-in exception types from earlier chapters. Here you will learn what an exception object tells you after you catch it, so you can diagnose a failure instead of only knowing that one happened.
The methods every exception has
Every exception in Apex, built-in or custom, inherits the same methods from the Exception class.
| Method | Returns |
|---|---|
| `getMessage()` | The error text. |
| `getTypeName()` | The fully qualified type name, such as `System.QueryException`. |
| `getCause()` | The exception that triggered this one, or `null`. |
| `getStackTraceString()` | The call stack as text, showing the classes and methods that were running. |
| `getLineNumber()` | The line number where the exception was thrown. |
| `setMessage(String)` | Replaces the message returned by `getMessage()`. |
Inspecting a caught exception
Catching the generic Exception lets you see which type actually arrived.
try {
Account a = [SELECT Id FROM Account WHERE Name = 'Does Not Exist' LIMIT 1];
System.debug(a.Id);
} catch (Exception e) {
System.debug('Type: ' + e.getTypeName());
System.debug('Message: ' + e.getMessage());
System.debug('Line: ' + e.getLineNumber());
System.debug('Stack: ' + e.getStackTraceString());
}Run this in Anonymous Apex and open the debug log. The type is System.QueryException, and the message says the list has no rows for assignment to an SObject. The line number points at the query, and the stack trace names where the code was running.
Following the cause chain
When code wraps one exception inside another, the original error sits behind getCause(). The outer message is often generic, so the useful detail is usually further down the chain. A helper can walk the whole chain.
public class ExceptionDescriber {
public static String describe(Exception e) {
List<String> lines = new List<String>();
Exception current = e;
while (current != null) {
lines.add(current.getTypeName() + ': ' + current.getMessage());
current = current.getCause();
}
return String.join(lines, '\n caused by ');
}
}The loop ends because getCause() returns null on the last exception in the chain. Wrapping is covered in detail in a later chapter.
Changing the message
setMessage replaces what getMessage() returns. This matters for AuraHandledException, whose message is otherwise not reliably readable in tests. Use it deliberately, because it hides the original text from any code that reads the message later.
Common mistakes
- Reading
getStackTraceString()and expecting a line number for every frame. The frames are text, so usegetLineNumber()for the throwing line only. - Comparing
getMessage()against exact text in logic. Message wording can change between releases, so branch on the exception type instead. - Calling
getCause()and assuming it is notnull. Most exceptions have no cause, so check first. - Logging only
getMessage(). Without the type and stack trace, the log does not show where the failure came from.
Exercise
A caught exception's message reads "Could not create account", which tells you little. Which method do you call to find the underlying error, and how do you know when you have reached the last exception in the chain?
Show answer
Call getCause() on the exception. It returns the exception that triggered the current one. Repeat the call on each result until getCause() returns null, which means you have reached the root cause.