Search code examples
oopexceptionabapsap-guinetweaver

SAPGUI not displaying long_text or text for unhandled custom exceptions


In SAP NetWeaver 7.52 I created an ABAP classed based exception that works fine while executing within a try catch clause in the report/program. But the custom message is not displayed in SAPGUI when the exception is not handled by a try catch clause.

What I'm looking for is that when no try catch is defined the exporting message used at the moment of the raise statements is shown in the "UNCAUGHT_EXCEPTION". I have tried redefining the get_text( ) and get_longtext( ) methods. But the ABAP Run time error does not give any useful information to the developer about the cause (which is stored in the "attr_message" attribute of the exception).

When using the "try catch" the message can be retrieved without problems, but the idea is that SAPGUI presents the developer the right message in the "ABAP Runtime Error" report.

zcx_adk_exception.abap

"! Base exception for all ABAP Development Kit (ADK) exceptions
class zcx_adk_exception definition public create public inheriting from cx_dynamic_check.

    public section.
        "! Initializes the exception message
        "! @parameter message    | Message related to the reason of the exception
        methods constructor
            importing value(message) type zadk_str optional.

        "! Returns the message associated to the exception
        methods get_message
            returning value(result) type zadk_str.

        methods if_message~get_text redefinition.

        methods if_message~get_longtext redefinition.

        private section.
            data attr_message type zadk_str value ''.

endclass.

class zcx_adk_exception implementation.

    method constructor ##ADT_SUPPRESS_GENERATION.
        super->constructor(  ).
        if message is not initial.
            me->attr_message = message.
        endif.
    endmethod.

    method get_message.
        result = me->attr_message.
    endmethod.

    method if_message~get_text.
        result = me->get_message(  ).
    endmethod.

    method if_message~get_longtext.
        result = me->get_message(  ).
    endmethod.

endclass.

What works fine:

try.
    raise exception type zcx_adk_exception exporting message = 'Base_Exception_Error'.
catch zcx_adk_exception into data(ex).
    write: / 'Example 1:', ex->get_message(  ).
    write: / 'Example 2:', ex->get_text(  ).
    write: / 'Example 2:', ex->get_longtext(  ).
endtry.

And the output is this:

enter image description here

What does not work:

" Not Catching the exception
raise exception type zcx_adk_exception exporting message = 'Base_Exception_Error'. 

This results in the following message being displayed instead

enter image description here


Solution

  • Following the previously proposed idea of using a message I came up with the following code that allows the exception to be raised with a message. This allows the exception to show the right message when called within a "try catch" block and display a useful message in the "Error analysis" section of the dump generated by SAPGUI.

    Solution:

    "! Program to test functionalities and utilities
    REPORT zsandbox_tests.
    
    
    " Exception Class
    CLASS lcl_exception DEFINITION INHERITING FROM cx_dynamic_check.
      PUBLIC SECTION.
        INTERFACES if_t100_dyn_msg.
        METHODS if_message~get_text REDEFINITION.
        METHODS constructor
          IMPORTING VALUE(message) TYPE string.
    
      PRIVATE SECTION.
        DATA attr_message TYPE string VALUE ''.
    ENDCLASS.
    
    CLASS lcl_exception IMPLEMENTATION.
      METHOD if_message~get_text.
        result = attr_message.
      ENDMETHOD.
    
      METHOD constructor.
        super->constructor(  ).
        me->attr_message = message.
      ENDMETHOD.
    ENDCLASS.
    
    
    
    " Class that raises the exception
    CLASS lcl_main DEFINITION.
      PUBLIC SECTION.
        CLASS-METHODS main RAISING lcl_exception.
    ENDCLASS.
    CLASS lcl_main IMPLEMENTATION.
      METHOD main.
        DATA raise_message TYPE string VALUE 'Custom Message for the Exception'.
        RAISE EXCEPTION TYPE lcl_exception
            MESSAGE e000(lcl_exception) WITH raise_message '' '' '' " The if_t100_dyn_msg supports 4 attributes: V1, V2, V3 and V4 but I only use the first one
            EXPORTING message = raise_message.
    
      ENDMETHOD.
    ENDCLASS.
    
    
    " Call to Main Method
    START-OF-SELECTION.
    
      TRY.
          lcl_main=>main( ).
        CATCH lcl_exception INTO DATA(ex).
          WRITE ex->get_text(  ).
      ENDTRY.
    

    This generates the following output: Output of try catch

    When no try catch is used:

    " Call to Main Method
    start-of-selection.
      lcl_main=>main( ).
    

    This is the output: SAPGUI dump