Catch Multiple Exceptions

Did you know that Exceptions can be chained?

You can chain exception catch!

Did you know that Exceptions can be chained?

apex
try {
    // do some stuff
} catch (Exception a) {
    // logic after A exception
} catch (Exception b) {
    // logic after B exception
}

It can be useful if you want to implement different strategies depending on a thrown exception:

apex
try {
    Example.doSomethingRisky();
} catch (NullPointerException nullProblem) {
    Helper.callSystemAdmin();
} catch (DmlException insertProblem) {
    System.debug('It\'s not a big deal!');
}

It also works with custom exceptions:

apex
try {
    Example.doSomethingRisky();
} catch (MyQueueStackException customException) {
    System.enqueueJob(new EmergencyQueueable());
} catch (LastHopeException hopeException) {
    GodClass.sendForHelp();
}

Text and code were extracted from the original slide. Plain-text version of the whole catalog