1

I have the following catch block:

    } catch(Exception ex) {
        Database.rollback(sp);
        RestContext.response.statuscode = (ex.getTypeName() == 'mainClass.DuplicateException') ? 409 : 400;
        ucr.message = ex.getMessage();
        if(RestContext.response.statuscode != 409)
            handleError.logError(mainClass.class, 'Error found', ex);
        
    }

Where ex return multiple types of exceptions (System.JSONException or System.DMLException and etc.) and keeps in the log in handleError

My goal is NOT to keep (not to log/save) System.JSONException.


1-st idea was to create a Scheduler Class to remove all JSON type logs. I did it, it works, but now I want to not to keep them even when exception occurs.


So I started to modify my catch block:

    } catch(System.JSONException ex) {
       //or return null or keep this line empty
    } catch(Exception ex) {
        Database.rollback(sp);
        RestContext.response.statuscode = (ex.getTypeName() == 'mainClass.DuplicateException') ? 409 : 400;
        ucr.message = ex.getMessage();
        if(RestContext.response.statuscode != 409)
            handleError.logError(mainClass.class, 'Error found', ex);
        
    }

I think code above will catch JSON type exception and will nothing. But in the second catch I will again will get JSON exception.

Should I add if statement in catch block saying that if ex will return JSON type exception do not keep it there?

I found that it is bad idea to put if statement in try catch block as well. So as I am learning, want to have any idea how to process.

Do not want just copy paste someones code, want to follow an idea

1 Answer 1

1

I think code above will catch JSON type exception and will nothing. But in the second catch I will again will get JSON exception.

Your first statement is correct, but the second is not. Once an exception handler (catch block) has been matched, no further catch blocks are considered. Note that they are effectively processed in declared order, so your code snippet is appropriate in having the general exception caught second.

NB: it is bad practice (indeed, considered an anti-pattern) to catch and completely ignore an exception since this can make identifying issues almost impossible. Even just using System.debug isn't good enough in general. If the JSON parsing indicates a misconfiguration you should consider logging it somewhere to allow admins to resolve the problem. If you are still going to quietly ignore these exceptions you should ensure there is a well argued reason captured in a comment in the (otherwise empty) catch block to explain this to maintainers of your code (though, again, I recommend against this).

It is always possible to re-throw the caught exception that you want to propagate to the caller, e.g.

catch (SomeException e) {
    throw e;
} catch (Exception e) {
    // handle all remaining types of exception
}

This means the caller only gets the re-thrown exception (e.g. your JSON parse issue) and can handle that at its own level as needed.

1
  • Thanks for reply. No, I am not going to ignore json exception at all. I just want to ignore them not to keep a lot of logs related to that. I will resolve it in other class which I will define. Thanks for sharing an idea! Jun 3, 2021 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .