Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

abap - Why run time error UNCAUGHT_EXCEPTION is triggered although I catch the exception?

I expect the following program to output an error message (via WRITE), but actually it triggers the run time error UNCAUGHT_EXCEPTION (short dump) at the time of RAISE EXCEPTION TYPE:

REPORT.
CLASS lcx_exception DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    METHODS main.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
  METHOD main.
    RAISE EXCEPTION TYPE lcx_exception.  "<===== HERE, run time error UNCAUGHT_EXCEPTION
  ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
  TRY.
    NEW lcl_app( )->main( ).
  CATCH lcx_exception INTO DATA(ex).
    WRITE: / 'Exception:', ex->get_text( ).
  ENDTRY.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...