exceptionabap

How to convert generic exception into BAPIRET2 message?


I have several custom exception classes that were created "With Message Class". Since I can't directly get a message from them, I want to create a utility method that returns a BAPIRET2 from a given exception based on the values in IF_T100_MESSAGE~T100KEY. However, I can't provide that method with a generic CX_ROOT importing parameter as this class is not message-enabled. I also can't create a generic message-enabled exception class as new classes have to inherit from one of CX_STATIC_CHECK, CX_DYNAMIC_CHECK, or CX_NOCHECK.

How can I then retrieve the message details from an unspecified exception? Should I create a method that receives a CX_ROOT and then does up to three calls to methods with an import typed to each of the three possible subclasses? Or are there better alternatives?


Solution

  • You could prepare a type descriptor of the interface (once):

    DATA: lr_t100_descr TYPE REF TO cl_abap_intfdescr.
    
    lr_t100_descr ?= cl_abap_typedescr=>describe_by_name( 'IF_T100_MESSAGE' ).
    

    and then examine each exception as it comes your way:

    DATA: lr_t100_exception TYPE REF TO if_t100_message.
    
    IF lr_t100_descr->applies_to( ir_any_exception ) = abap_true.
      lr_t100_exception ?= ir_any_exception.
      " ...
    ENDIF.