Handle Uncatchable Exceptions in Batch

However, there is another way to achieve this in Apex Batches. Check the next page to see how.

Some types of exceptions cannot be handled using standard try - catch blocks. Those types are: Limit Exception and Assert Exception:

apex
try {
    throw new System.LimitException();
} catch(System.LimitException ex) {
    System.debug('This code will never be executed');
} finally {
    System.debug('Neither will this one');
}

However, there is another way to achieve this in Apex Batches. Check the next page to see how.

All changes that needs to be implemented in batch is the implementation of the Database.RaisesPlatformEvents interface:

apex
public with sharing class BatchExample implements Database.Batchable<SObject>,
Database.RaisesPlatformEvents {
    //Code of your batch
}

Now you can subscribe to this event stream using Apex Trigger or Flow:

apex
trigger MarkDirtyIfFail on BatchApexErrorEvent (after insert) {
    // Trigger Logic
}

BatchApexErrorEvent contains information about Async Apex Job Id, Exception Type, JobScope (all records Ids), Message, Phase of the batch (Start, Execute, Finish) and Stack Trace. Developer can use that information for e.g. error logging, retry mechanisms, user notifications

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