1

Why is this Exception not being caught?

I read that System.LimitException is not caught but didn't see anywhere about System.FinalException.

Non-Working Code

@AuraEnabled
public static List<RecordType> getRecordTypes() {

    try {

        String sObjectType = 'Task';

        List<RecordType> recordTypes = RecordTypeRepository.getBySObjectType(sObjectType);

        return removeGenericTaskRecordType(recordTypes);

    } catch (Exception e) {

        String message = ErrorMessage.formatExceptionMessage(e, 'There was an error running NewTaskFormController getRecordTypes.');
        System.debug(message);
        throw new AuraHandledException(message);
    }
}


public static List<RecordType> removeGenericTaskRecordType(List<RecordType> recordTypes) {

    RecordType genericTask = RecordTypeRepository.getById(RecordTypeRepository.GENERIC_TASK_ID);

    Integer genericTaskIndex = recordTypes.indexOf(genericTask);

    for (RecordType recordType: recordTypes) {
        recordTypes.remove(genericTaskIndex );
    }


    return recordTypes;
}

What I've Tried

  • The same code structure (try-catch) just a different exception and it does get caught. "common.apex.runtime.impl.ExecutionException: List index out of bounds: -1

Working-Code Example

 public static List<RecordType> removeGenericTaskRecordType(List<RecordType> recordTypes) {

    RecordType genericTask = RecordTypeRepository.getById(RecordTypeRepository.GENERIC_TASK_ID);

    Integer genericTaskIndex = -1;

    recordTypes.remove(genericTaskIndex);

    return recordTypes;
}
4

Your exception is not being caught because FinalException is not catchable. Neither are LimitException nor AssertException.

You can quickly check for yourself if a given type of exception is catchable using an anonymous script like the following:

try
{
    throw new FinalException();
}
catch (Exception pokemon)
{
    system.debug('Cannot catch em all');
}

Note that in general:

  • you should know what specific types of exception you are expecting and catch only those
  • it is better to avoid the exception entirely if it is preventable, even if it can be caught
  • It's also very avoidable; just using a normal for loop instead of for-each fixes the problem. – sfdcfox Jan 3 at 16:29
  • Yeah I thought that might be a bit of a separate question though. Why can I not catch FinalException seems like one for which we might get plenty of dupes over the years. – Adrian Larson Jan 3 at 16:30
  • Yeah, I'm kind of surprised this is not a duplicate (but apparently not?). Good to have an answer like this available. – sfdcfox Jan 3 at 16:35
  • Thank you. For some reason the Docs only mention LimitException and AssertException but not FinalException. – shmuels Jan 3 at 18:31
  • 1
    It does not prove the difference between those two, but in general, if a standard exception is not caught with this script, you can assume it is by design. – Adrian Larson Jan 3 at 18:42
2

Answer from @Adrian is good, but on a different note the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = accts.size()-1;  i>=0 ; i--) {
    Account a = accts[i];
}
  • @AdrianLarson Thanks for suggestions. I should have looked more closely, I lifted off from the refered article. Fixed it now! – codeyinthecloud Jan 3 at 16:48
  • 1
    Thank you. I knew why I was getting the Exception and how to prevent it. I just wasn't sure if there was something wrong with my try-catch, if this was a Salesforce bug or as @AdrianLarson confirmed that all is good and it's the intended behavior. – shmuels Jan 3 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.