I use a CustomException
and can't get the stack trace to work. I meticulously followed and implemented the Salesforce Developer Guide for Custom Exceptions but my debug log simply shows an empty stack trace.
11:30:40:294 USER_DEBUG [8]|DEBUG|Line number: 22
11:30:40:295 USER_DEBUG [9]|DEBUG|Stack trace: ()
This makes no sense at all. If I catch one of the standard exceptions, I can read that property. Posts in developer boards clearly state, that getStackTraceString()
should work on both custom and system exceptions. So why is my debug log different? What am I doing wrong?
Code for Custom Exception:
public class MerchandiseException extends Exception {
}
Code that throws the exception:
public class MerchandiseUtility {
public static void mainProcessing() {
try {
insertMerchandise();
} catch(MerchandiseException me) {
System.debug('Message: ' + me.getMessage());
System.debug('Cause: ' + me.getCause());
System.debug('Line number: ' + me.getLineNumber());
System.debug('Stack trace: ' + me.getStackTraceString());
}
}
public static void insertMerchandise() {
try {
// Insert merchandise without required fields
Lead l = new Lead();
insert l;
} catch(DmlException e) {
// Something happened that prevents the insertion
// of Employee custom objects, so throw a more
// specific exception.
throw new MerchandiseException(
'Merchandise item could not be inserted.', e);
}
}
}
I don't have a "Merchandise__c" Object, thats why I decided to create a lead object which will throw the exact same DMLException (since LastName is required and not set). If I change the code to re-throw an arbitrary System Exception (i.e. NullPointerException) and catch a generic Exception in the outer try/catch, debug log works flawlessly:
12:11:08:174 USER_DEBUG [8]|DEBUG|Line number: 22
12:11:08:174 USER_DEBUG [9]|DEBUG|Stack trace: Class.MerchandiseUtility.insertMerchandise: line 22, column 1
12:11:08:000 USER_DEBUG Class.MerchandiseUtility.mainProcessing: line 4, column 1
12:11:08:000 USER_DEBUG AnonymousBlock: line 1, column 1
12:11:08:000 USER_DEBUG AnonymousBlock: line 1, column 1