How to retrieve the root exception when receiving the error message “An exception was raised (Message No. SY530)”

When developing sample code it often happens that an underlying framework raises an exception that was not caught by the API itself that I have called in my code.

As a result you only get an unspecific error messages such as

An exception was raised (Message No. SY530)

If you are working on SAP NetWeaver 753 or later (that means SAP S/4HANA 1809) on premise or SAP Cloud Platform ABAP Environment there is a new method available as pointed out by Dominik Bigl (see his valuable comment below).

For older releases you can use a small method get_root_exception that I usually add to my test classes so that I get the longtext of the exception that was originally called and that caused the issue.

CLASS zcl_get_root_exception DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.

    METHODS get_root_exception
      IMPORTING
        !ix_exception  TYPE REF TO cx_root
      RETURNING
        VALUE(rx_root) TYPE REF TO cx_root .

ENDCLASS.



CLASS zcl_get_root_exception IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.
    TRY.
        DATA num1 TYPE i VALUE 2.
        DATA num2 TYPE i VALUE 0.
        DATA(result) = Num1 / Num2.
        out->write( result ).
      CATCH cx_root INTO DATA(lx_exception).
        out->write( 'root exception: ' && get_root_exception( lx_exception )->get_longtext(  ) ).
    ENDTRY.
  ENDMETHOD.

  METHOD get_root_exception.
    rx_root = ix_exception.
    WHILE rx_root->previous IS BOUND.
      rx_root ?= rx_root->previous.
    ENDWHILE.
  ENDMETHOD.

ENDCLASS.

Original Article:
https://blogs.sap.com/2020/05/01/hot-to-retrieve-the-root-exception-when-receiving-the-error-message-an-exception-was-raised-message-no.-sy530/

ASK SAP EXPERTS ONLINE
Related blogs

LEAVE A REPLY

Please enter your comment!
Please enter your name here