Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting call stack / stack trace of caught exception

I'd like to get the call stack of an exception which were caught in a try block to identify the location were the exception was thrown. So basically, I want to get the same information which would be visible for a short dump in ST22 under section "Active Calls/Events".

In other words, I'm asking for the ABAP equivalent to Throwable.getStackTrace() in Java.

I know that there is function module SYSTEM_CALLSTACK, but I tried this within a catch block and just contains the call stack of the current method where the catch block is located, but not the call stack of the exception.

Is there any way to get the call stack for an exception in ABAP?

1 ACCEPTED SOLUTION

vwegert
Active Participant

You have to use the BEFORE UNWIND addition to the CATCH clause:

REPORT z_catchy_catchy_callstack.

CLASS lcl_foo DEFINITION.
  PUBLIC SECTION.
    METHODS main.
    METHODS method_raising_exception.
ENDCLASS.

CLASS lcl_foo IMPLEMENTATION.

  METHOD main.
    TRY.
        method_raising_exception( ).
      CATCH BEFORE UNWIND cx_root.
        DATA callstack TYPE sys_callst.
        CALL FUNCTION 'SYSTEM_CALLSTACK'
          IMPORTING
            et_callstack = callstack.
        cl_demo_output=>new( )->write_data( value = callstack )->display( ).
    ENDTRY.
  ENDMETHOD.

  METHOD method_raising_exception.
    RAISE EXCEPTION TYPE cx_abap_invalid_value.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  NEW lcl_foo( )->main( ).
8 REPLIES 8

Sandra_Rossi
Active Contributor
0 Kudos

All exceptions have the method GET_SOURCE_POSITION. But it only returns information about the procedure where the exception occurs, not the full stack. Might it be sufficient?

0 Kudos

HI Sandra, thanks for your answer, but I'd be indeed be interested in the complete stack. Source position would be a starting point, but when the exception e.g. occurs in standard code, I won't be able to identify the last step in our custom code which would be of more interest to me.

0 Kudos

If you're interested in the custom code only, then you may intercept the exception in the procedure where the standard code is called, and raise a custom exception with PREVIOUS parameter set to the standard exception (you'll have then a chain of exceptions, with the corresponding raised exception locations)

0 Kudos

I fully agree - the call to the standard code should be already wrapped in an exception. But I'm exactly interested in debugging possibility (other than ST22) for a situation where this exception chaining is missing.

0 Kudos

If you display the triggering source position (button in the debugger), add a breakpoint there, restart the process (provided it's reproducible, but what can we do with non-reproducible things...) and you'll see the call stack via the debugger. Note that the debugger has also an option to generate the exception object even if it's not requested in the ABAP code.

0 Kudos

Hi Sandra, invoking the debugger (or any other SAP tool) does not work for my use case. My intend is to pass the call stack information when executing an RFC function module from outside of the SAP system and I'd like to have these information in the calling external system log.

vwegert
Active Participant

You have to use the BEFORE UNWIND addition to the CATCH clause:

REPORT z_catchy_catchy_callstack.

CLASS lcl_foo DEFINITION.
  PUBLIC SECTION.
    METHODS main.
    METHODS method_raising_exception.
ENDCLASS.

CLASS lcl_foo IMPLEMENTATION.

  METHOD main.
    TRY.
        method_raising_exception( ).
      CATCH BEFORE UNWIND cx_root.
        DATA callstack TYPE sys_callst.
        CALL FUNCTION 'SYSTEM_CALLSTACK'
          IMPORTING
            et_callstack = callstack.
        cl_demo_output=>new( )->write_data( value = callstack )->display( ).
    ENDTRY.
  ENDMETHOD.

  METHOD method_raising_exception.
    RAISE EXCEPTION TYPE cx_abap_invalid_value.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  NEW lcl_foo( )->main( ).

0 Kudos

Hello Volker,

many thanks for that hint, that was exactly the information I was looking for! Now I get the call stack as expected.

Regards,
Christian