The solution is to add RAISING lcx_exception
(eventually more exception classes may be listed here) to the method declaration, so that the exception is automatically propagated to the calling procedure (and caught, hopefully) when it occurs:
REPORT.
CLASS lcx_exception DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
METHODS main RAISING lcx_exception. "<==================== HERE
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD main.
RAISE EXCEPTION TYPE lcx_exception.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
NEW lcl_app( )->main( ).
CATCH lcx_exception INTO DATA(ex).
WRITE: / 'Exception:', ex->get_text( ).
ENDTRY.
Note that you can't list the exception classes which inherit from the root class CX_NO_CHECK
, directly or indirectly, because these exceptions are propagated implicitly. Only exception classes inheriting from CX_STATIC_CHECK
and CX_DYNAMIC_CHECK
, directly or indirectly, need to be indicated after RAISING
for being propagated. Those 2 root classes may also be indicated to propagate all exception classes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…