0

If we are using try catch only on DML operation part, where on catch block if only DmlException is used, then would it be enough to handle all possible errors occurs there or we also need to use general Exception?

Example:

List<Contact>contactToUpdate = new List<Contact>();

List<Contact>conList = [Select id, Name from Contact];
for(Contact con : conList){
    con.Name = con.Name+'-Test';
    contactToUpdate.add(con);
}

try{
  update conList;
}
catch(DmlException e){
System.debug('Error: '+e.getDmlId(0)+'-'+e.getDmlMessage());
}

Why I am looking to use DmlException?

Since I need to get the id of the record for which error occurs. I could have used Database.update to collect id and error using SaveResult but I don't want to partially update the records. And with default option to database.update (which is true) will throw exception only.

2 Answers 2

1

Historically, a DML transaction in Apex would just pass the original exception up the stack (i.e. it was possible to get StringException, NullPointerException, etc from a DML statement). At some point, salesforce.com changed this behavior such that anything aside from LimitException (which you can't catch anyways) will always be a DmlException. It is perfectly safe to use only DmlException.

However, remember that you can use partial update to collect all errors, and rollback if necessary:

SavePoint sp = Database.setSavePoint();
Database.UpdateResult[] results = Database.update(conList, false);
Boolean shouldRollback = false;
for(Integer i = 0; i < conList.size(); i++) {
  Database.UpdateResult result = results[i];
  Boolean isSuccess = result.isSuccess();
  shouldRollback |= !isSuccess;
  if(!isSuccess) {
    System.debug(result.getErrors()[0].getMessage());
  }
}
if(shouldRollback) {
  Database.rollback(sp);
}
3
  • 1
    shouldRollback |= isSuccess; should be shouldRollback |= !isSuccess;: the |= operator returns false only if both operands are false, so currently it will rollback if at least one result is successfull, therefore if everything went good a rollback would still happen, while if everything went bad and every isSuccess is always false, no rollback would happen.
    – RubenDG
    Jul 11, 2023 at 15:06
  • @RubenDG Yeah, that was a typo. Thanks for letting me know!
    – sfdcfox
    Jul 11, 2023 at 15:43
  • Thanks sfdcfox for your answer. This is what I was looking for 🙂. Rollback will not apply for me, since I will be using the logic in batch class. Anyway thanks again!!
    – ItsVicky
    Jul 12, 2023 at 18:40
0

You need to keep in mind the overall transaction that might happen if you are inserting any record. On insertion of contact there might be other processes running, for example a trigger or a workflow etc. An exception might be raised from these processes. Hence, it's always better to have a generic exception handler and to add to it you can write a specific catch block for specific exception handling and processing.

1
  • Such case which you mentioned, for example from Trigger errors will be cought by DmlException. I have tried this. I have updated my question to add why I am looking to use DmlException instead of generic one.
    – ItsVicky
    Jul 11, 2023 at 11:01

You must log in to answer this question.

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