Sub topic 5
Multiple catch blocks and the generic Exception
This chapter assumes you can write a basic try/catch block, know that Apex has built-in exception types such as DmlException and QueryException, and understand that every exception type inherits from Exception.
Why use more than one catch block
A single try block can fail in different ways. A query can return no rows, a DML statement can fail validation, and a null reference can appear. Each failure may need a different response. Apex lets you attach several catch blocks to one try block, and each block names the exception type it handles.
try {
Account a = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
a.Name = null;
update a;
} catch (QueryException e) {
System.debug('No account found: ' + e.getMessage());
} catch (DmlException e) {
System.debug('Update failed: ' + e.getMessage());
} catch (Exception e) {
System.debug('Unexpected error: ' + e.getTypeName());
}Apex checks the catch blocks from top to bottom. It runs the first block whose type matches the thrown exception, then skips the rest. After that block finishes, any finally block runs.
Catch order matters
A catch block for a parent type also matches every child type. Exception is the parent of all exception types. If you put catch (Exception e) first, no later block can ever run. Apex rejects this at compile time with an error saying the exception is already handled by an earlier block.
The rule: list the most specific types first and the most general type last.
try {
insert new Account();
} catch (Exception e) {
System.debug('Generic handler');
} catch (DmlException e) {
// Compile error: this block can never be reached.
System.debug('DML handler');
}The generic Exception as a safety net
A final catch (Exception e) handles anything the earlier blocks did not match. It is useful at boundaries, such as a controller method or a trigger helper, where an unhandled error would show users a raw system message.
Use it carefully:
- Log or report the error, or rethrow it. Do not leave the block empty, because that hides real defects.
- Do not use it to replace specific handlers. Specific blocks let you recover in specific ways.
- Remember that some failures cannot be caught at all, such as governor limit violations. A generic handler does not protect you from those.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| `catch (Exception e)` placed first | Compile error, later blocks unreachable | Move it to the end |
| Empty catch block | Failures vanish silently | Log, rethrow, or handle the error |
| Reusing the same variable name in nested blocks | Compile error for a duplicate variable | Use distinct names such as `e` and `inner` |
| Catching `Exception` to detect a known case | Hard to tell causes apart | Catch the specific type |
How matching works
The following diagram shows how Apex picks a handler.
Exercise
Why does this code fail to compile? try { ... } catch (Exception e) { ... } catch (QueryException e) { ... }
Show answer
Exception is the parent of QueryException, so the first block already handles every query error. The second block is unreachable. Put catch (QueryException e) before catch (Exception e).