Many times you see in Apex code try/catch where you just system debug the exception like this:
try {
// do stuff
} catch (Exception e) {
System.debug(e);
}
You are catching the exception and system debugging it, this means that you are in essense swallowing and hiding the exception because no one will ever look at that log. You will have silent failures happening in your org. If you don’t need to take action on an exception at least persist the error to an log object where you can review and act upon it.
try/catch should be used a high up in the code, in Salesforce this would usually be Aura, LWC or VF. There you should use try/catch and handle and show the error to the user (possibly formatting and showing an user friendly error) so that the user can review and act upon it.

Leave a comment