This question definitely is a duplicate of many questions here, but after reading most of them I am still somewhat clueless.
Question 1: why I can't just catch exceptions in the execute and send an email out? Would a) Batch help, or b) checking or c) writing a custom Error object?
public class MyQueue implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
try {
Integer invalid = 100 / 0;
}
catch(Exception ex) {
sendEmail(ex);
}
}
}
Question 2: Can I test it like this?
@IsTest
private class MyQueue_Test {
@IsTest
private static void sendsEmailOnError() {
// Exercise
Test.startTest();
System.enqueueJob(new MyQueue());
Test.stopTest();
// Verify
System.assertEquals(1, Limits. getEmailInvocations());
}
}