Sub topic 8
Wrapping and rethrowing with a cause
This chapter assumes you can write try, catch, and finally blocks, define a custom exception class, and read basic exception methods such as getMessage. It covers what to do when a low-level exception reaches a layer that should speak in its own terms.
Why wrap an exception
A service class should not expose every detail of how it works. If AccountService.create throws a raw DmlException, every caller must know that the service uses DML. Callers would also have to catch built-in types that may change when you rewrite the service.
Wrapping fixes this. You catch the low-level exception and throw your own type. You pass the original exception as the cause, so its details are not lost.
Passing a cause
Every custom exception that extends Exception gets four constructors automatically: no arguments, a message, a cause, and a message plus a cause. You do not declare them.
public class AccountService {
public class AccountServiceException extends Exception {}
public static void create(Account acc) {
try {
insert acc;
} catch (DmlException e) {
throw new AccountServiceException('Could not create account', e);
}
}
}The caller now catches one type that belongs to the service:
try {
AccountService.create(new Account());
} catch (AccountService.AccountServiceException e) {
System.debug('Message: ' + e.getMessage());
Exception cause = e.getCause();
System.debug('Cause type: ' + cause.getTypeName());
System.debug('Cause message: ' + cause.getMessage());
}getCause returns an Exception. To use DML-specific methods, check the type and cast it:
if (e.getCause() instanceof DmlException) {
DmlException dmlEx = (DmlException) e.getCause();
System.debug(dmlEx.getDmlMessage(0));
}Rethrowing unchanged
Sometimes you only need to clean up, then let the same exception continue. Use throw e; inside the catch block. The callers see the original type and message. Use this when the exception type is already meaningful to the caller, as with the savepoint example later in this tutorial.
Diagnosing a wrapped failure
The wrapper has its own stack trace, which starts where you created it. The original failure point lives in the cause. When debugging, log both:
System.debug(e.getStackTraceString());
if (e.getCause() != null) {
System.debug(e.getCause().getStackTraceString());
}Always check getCause() for null. An exception created without a cause returns null, and calling a method on it throws a NullPointerException (System.NullPointerException).
Common mistakes
- Throwing a new exception with only
e.getMessage(). You lose the original type and stack trace. Passeas the cause. - Wrapping at every layer. Each wrapper adds a level to dig through. Wrap where you cross a boundary, such as from data access to service logic.
- Catching
Exceptionjust to wrap it. This hides programming errors like null references. Catch the specific types you expect.
Exercise
A method catches a DmlException and executes throw new AccountServiceException(e.getMessage());. What information is lost, and how do you fix it?
Show answer
The original DmlException object is lost, including its type, its stack trace, and the per-record DML methods such as getDmlFieldNames. Fix it by passing the exception as the cause: throw new AccountServiceException('Could not create account', e);. Callers can then use getCause to reach the original.