8

I have code that is wrapped into a main try-catch block and then inside of that I have another try-catch which I am using for a specific use case. If the inner try-catch gets triggered I then use a custom exception to re-throw the caught exception. However re-throwing the exception loses its stackTrace unless I invoke the getStackTraceString() method.

Here's what I have:

public class myClass {
    public myClass() {}

    public void someMethod() {
        try {
        // some code
            try {
                // more code
            } catch (Exception ex) {
                String additionalDescription = 'something';
                throw new myClassException(additionalDescription, ex);
            }
        } catch (Exception ex) {
            system.debug('getStackTraceString is ' + ex.getStackTraceString());
        }
    }
    public class myClassException extends Exception {}
}

In this case the ex.getStackTraceString() comes in as blank. I get Caused by ().

However if I invoke ex.getStackTraceString() in the inner catch block then the other catch block can see the stack trace as well.

Here's an example when the stack trace is visible to the outer catch.

public class myClass {
    public myClass() {}

    public void someMethod() {
        try {
        // some code
            try {
                // more code
            } catch (Exception ex) {

                // Just invoking this method without debugging it 
                // or anything else allows the outer catch to read the trace as well. 
                ex.getStackTraceString();


                String additionalDescription = 'something';
                throw new myClassException(additionalDescription, ex);
            }
        } catch (Exception ex) {
            system.debug('getStackTraceString is ' + ex.getStackTraceString());
        }
    }
    public class myClassException extends Exception {}
}

Any ideas why? This class gets invoked by a trigger if that matters.

  • 4
    Could be possibly related to this known issue. However the issue description does not have a workaround, whereas you seem to have one. – Jayant Das Apr 10 at 20:12
  • Does the Exception cross the trigger boundary? E.g. are you doing something like try { insert records; } catch(MyClassException e) { ... }? – sfdcfox Apr 10 at 20:16
  • @sfdcfox No, I am not. Just querying for objects but not modifying them. – Arthlete Apr 10 at 20:39
  • 1
    @JayantDas Thanks for sharing, it seems like the same thing. – Arthlete Apr 10 at 20:41
  • 1
    @Arthlete that was the first result returned when I did a web search :) You should in fact reach out to support and let them know that there's indeed a workaround as you found out, so that they can update the issue. I was able to replicate the behavior as you mentioned. I have seen such weird behavior at times with the platform where just making a method call resolves, one of such that we experienced made to this known issue. – Jayant Das Apr 10 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.