3

I've set out to build a custom error handling and logging feature for my org wherein all apex exceptions will be logged into a custom sObject for analysis and follow-up.

public class TriggerException extends Exception {

    public TriggerException(String message, Exception ex, String className, String methodName) {
        this(message, ex);
        ExceptionLog__c log = new ExceptionLog__c(
            ClassName__c = className,
            MethodName__c = methodName,
            ErrorLine__c = errorLine,
            ErrorMessage__c = errorMessage,
            ErrorDetails__c = errorStackTrace,
            ErrorType__c = errorType
        );
        insert log;
    }

}

However, this doesn't ever seem to save the ExceptionLog__c and I'm assuming it's because it's also caught up in the same database rollback procedure as the rest of the code...

How can I do this without having to add UtilClass.logMe(...); to every catch {} block in my org??

4

I would not suggest you to insert record everytime in method (Code not bulkified), If this method is called in loop, You may run into DML governor limit and therefore exception handling will cause exception.

Here, is sample utility class [pseudocode]

public class HandleException
{
private static list<ExceptionLog__c> lstException = new list<ExceptionLog__c>();

public void addException (String message, Exception ex, String className, String methodName)
{
    ExceptionLog__c log = new ExceptionLog__c(
        ClassName__c = className,
        MethodName__c = methodName,
        ErrorLine__c = errorLine,
        ErrorMessage__c = errorMessage,
        ErrorDetails__c = errorStackTrace,
        ErrorType__c = errorType
    );
    lstException.add(log); 
}

public static void saveExceptionLog()
{
    if(!lstException.isEmpty())
    {
        Database.insert(lstException,false); 
    }
  }
 }

You should call method saveExceptionLog() at end of your class or Trigger, If there is any unhandeled exception then even this approach will not work. We have to add try and catch block at all possible places.

Once object can have around 25 lookup fields, If you want more granular logging then you can create lookup for record where it occurred. This is very helpful for end to end reporting purpose and to reproduce issue with Data.

  • That's a fair point about bulkification, and a good reason to not handle it within the Exception class itself... I will go back to the failover way of catch (Exception e) {log(e)} – Nathan Williams Jun 23 '15 at 16:30
  • @jitendra zaa do we have to call both the static methods inside the catch block ? – Jarvis Dec 11 '15 at 1:53
  • 1
    @Bennie If you have multiple catch blocks, then call addException() methods in all catch block and at end of transaction, call saveExceptionLog() as it uses DML. Calling multiple time may adversely effect DML – Jitendra Zaa Dec 11 '15 at 18:47
  • @JitendraZaa What if I use a future method to write a custom object that could catch the error details. As future method will have more resources we can avoid calling these two separate methods for logging and saving exceptions in each class. – Jarvis Jan 27 '16 at 6:53

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.