It was to my understanding that when you throw an exception all previous operations are rolled back.

When an exception occurs, code execution halts and any DML operations that were processed prior to the exception are rolled back and aren’t committed to the database. Exceptions get logged in debug logs.

I am running into a situation where I am able to insert a record in a logger class in the catch and throw an exception after it. What gives?

The flow is: Class A calls Class B -> Class B calls Class C -> Class C has a webservice and a try/catch

public HttpResponse postTokenizer(String securityToken, String payloadJSON) {
    try {
        if (payloadJSON == null) {
            throw new GenericException('Tokenizer was called with no IDs. The ID must be either a Contact or Lead.');
        HttpRequest tokenizerRequest = new HttpRequest();
        tokenizerRequest.setHeader('Authorization', 'token ' + securityToken);
        tokenizerRequest.setHeader('Content-Type', 'application/json');
        Http http = new Http();
        HttpResponse response = http.send(tokenizerRequest);
        if (!WebHelper.isSuccessStatus(response)) {
            throw new GenericException('There has been an error calling the tokenizer service.');
        return response;
    } catch(Exception e) {
        NFLogger.logError('TokenizerHelper, tokenizer', 'Call to postTokenizer() failed.', e); //todo:: how is this inserting????? shouldn't this be rolled back?
        throw e;


global with sharing class NFLogger {
private enum LogLevel {Error, Info}

public static void logError(String tags, String errorMessage){
    logMessage(logLevel.Error, tags, errorMessage, null);

public static void logError(String tags, String errorMessage, System.Exception ex){
    logMessage(logLevel.Error, tags, errorMessage, ex);

public static void logInfo(String tags, String infoMessage){
    logMessage(logLevel.Info, tags, infoMessage, null);

private static void logMessage(LogLevel level, String tags, String message, System.Exception ex){
    try {
        LogMessage__c newMessage = new LogMessage__c(level__c = level.name(), tags__c = tags, message__c = message, exception__c = ex.getMessage());
        insert newMessage;
    catch (Exception e){
        //intentionally we are ignoring "we suppress" any exception
        //we need to guarantee that logMessage will never throw any exceptions
  • Does it use Platform Events?
    – Adrian Larson
    Nov 12, 2018 at 20:15
  • How does NFLogger work? It could be using platform events to get outside of the transaction to avoid the rollback. Or if a higher class in the callstack is catching the exception the transaction won't be rolled back. Nov 12, 2018 at 20:15
  • @AdrianLarson no platform events. I will update the post with the NFLogger class
    – Olivia
    Nov 12, 2018 at 20:20

1 Answer 1


The transaction will only be rolled back automatically if the thrown exception isn't caught.

Your example showed a call stack like:

  1. Class A calls Class B
  2. Class B calls Class C
  3. Class C attempts a callout to a webservice.
    • It catches any resulting exceptions,
    • does DML to log the exception details,
    • and then throws the exception again.

Since the resulting LogMessage__c record exists after the transaction, there must be a catch in either class B or A that is handling the exception thrown by class C. This prevents the exception terminating the transaction and causing a rollback of any DML that had occured.

It looks like your quote came from Exceptions in Apex, What Happens When an Exception Occurs?. The last part of that paragraph clarifies that the transaction rollback is just for unhandled exceptions. It is probably a bit misleading to imply that a handled/caught exception would also cause a rollback.

What Happens When an Exception Occurs?

When an exception occurs, code execution halts. Any DML operations that were processed before the exception are rolled back and aren’t committed to the database. Exceptions get logged in debug logs. For unhandled exceptions, that is, exceptions that the code doesn’t catch, Salesforce sends an email that includes the exception information. The end user sees an error message in the Salesforce user interface.

  • The thing that perplexes me is that the only two locations in which there is a try/catch in the entire process is on the webservice method and on the root method. Does this mean that because there is a second catch on the root method, that this "handles" the webservice exception thus logging both exceptions to NFLogger?
    – Olivia
    Nov 12, 2018 at 21:19
  • and if this is the case, then I should take off the exception handling on the webservice method and solely put logging and exception handling on root methods...
    – Olivia
    Nov 12, 2018 at 21:21
  • If in doubt, check the Apex debug log. With an appropriate Apex Code logging level it will show you the flow of execution, including how the exceptions are being handled. I don't know what your root method looks like. I assume it is on class A and is the entry point for the transaction. Does it call NFLogger.logError() and then not throw anything? That seems like the likely scenario here. Nov 12, 2018 at 21:34
  • How you handle the exceptions is up to you. Generally, it probably wouldn't make sense to log the same exception twice. Also, rethrowing the exception in postTokenizer means the call stack trace changes, which will make it harder to debug the actual cause. Nov 12, 2018 at 21:35

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.