Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Andre_Fischer
Product and Topic Expert
Product and Topic Expert
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.bigl2 (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.
3 Comments