Best Practices for Exception Handling in Salesforce Apex

Sharing is Caring

What’s an Exception

An exception is an unexpected event that occurs when code is executing. Basically apex is signalling there is a problem and that it can’t deal with the issue.

Salesforce has about twenty different exceptions that can be thrown when there is a problem.

What Causes Exceptions

In a lot of cases within Salesforce, the error is most likely related to doing a callout, a null object, or when doing some sort of dml operation. Most exceptions are preventable by having a good, reliable, and repeatable programming process.

The most common exceptions are usually these exceptions:

  • DmlException are usually the result of a required field not being set, a record that’s been deleted, etc. I find this error is usually caused by a new validation rule being created or when I’m creating test data.
  • QueryException is usually the result of getting back a list instead of a single item. Usually this means the query is returning more than one result.
  • NullPointerException usually caused by not initializing a variable and then trying to do some sort of operation on it.

How Exception Handling Works

Apex implements the relatively standard try, catch and finally blocks that most C++ variants have implemented. As Apex is based off of Java, it works in basically the exact same way.

try {
doSomething();
}
catch (Exception ex) {
system.debug('Uh oh an error occurred!');
}

Apex also supports the finally block which is always called regardless of whether an exception has occurred. Finally is usually used to do some cleanup of resources.

try {
doSomething();
}
catch (Exception ex) {
system.debug('Uh oh an error occurred!');
}
finally {
doSomeCleanup();
}

Best Practices for Exception Handling in Apex

  • Never just catch Exception, instead catch the specific type of exceptions. For example, QueryException should be caught if you know it’s possible you might not have any records in the query. I commonly use a pattern like this for finding if there’s any associated tasks:
  • try {
    tskId = [select Id from Task Where AccountId =: accId];
    }
    catch (QueryException  ex) {
    tskId = null
    }
    
  • Exceptions in Apex should be handled carefully as they may mask some serious problems that may not be caught for a long time. Try catch blocks should be used sparingly because they can cause some serious data integrity issues.
  • Don’t be scared to use the System.SavePoint() and Database.rollback() functions.The key advantage of doing a rollback is that we can set the database back to a known state and not necessarily worry about whether an admin adds a validation rule or not.
  • Proper exception handling is usually a lot more than using try-catch blocks, because it isn’t necessarily bulkified. In general, library classes and triggers should contain very few if any exception handling.
  • try-catch blocks should very small as the larger the block is the more errors it is likely to be hiding.
Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.