&---------------------------------------------------------------------
*& Report ZYY_PRG_001
&---------------------------------------------------------------------
*&
&---------------------------------------------------------------------
REPORT ZYY_PRG_001 NO STANDARD PAGE HEADING.
*DATA result TYPE p DECIMALS 2.
- DATA oref TYPE REF TO cx_root.
- DATA text TYPE string.
- DATA i TYPE i.
- TRY .
-
i = 1 / 0.
- CATCH cx_root INTO oref.
-
text = oref->get_text( ).
-
WRITE : 'error : ', text.
-
RAISE EXCEPTION oref. "注:如果有此句,则不能放在Function中,否则报:Old and class-based exceptions must not be used the same time
- ENDTRY.
*------------------------
*DATA: exc TYPE REF TO cx_sy_dynamic_osql_semantics,
-
text TYPE string.
*TRY.
- RAISE EXCEPTION TYPE cx_sy_dynamic_osql_semantics
-
EXPORTING textid = cx_sy_dynamic_osql_semantics=>unknown_table_name token = 'Test'.
- CATCH cx_sy_dynamic_osql_semantics INTO exc.
- text = exc->get_text( ).
- MESSAGE text TYPE ‘I’.
*ENDTRY.
*---------------------------------------------------------------
PARAMETERS number TYPE i.
DATA result TYPE p DECIMALS 2.
DATA oref TYPE REF TO cx_root.
DATA text TYPE string.
START-OF-SELECTION.
TRY.
IF ABS( number ) > 100.
RAISE EXCEPTION TYPE cx_demo_abs_too_large.
ENDIF.
TRY.
result = 1 / number.
WRITE: / ‘Result of division:’, result.
result = SQRT( number ).
WRITE: / ‘Result of square root:’, result.
CATCH cx_sy_zerodivide INTO oref."输出0时会在这里捕获
text = oref->get_text( ).
CONCATENATE 'CATCH cx_sy_zerodivide : ’ text INTO text.
CLEANUP."当内层TRY发生了异常,且没有被捕获到,抛到外层TRY前会被执行
CLEAR result.
WRITE: / ‘cleanup’.
ENDTRY.
CATCH cx_sy_arithmetic_error INTO oref."输入负数会在这里捕获
text = oref->get_text( ).
CONCATENATE 'CATCH cx_sy_arithmetic_error : ’ text INTO text.
CATCH cx_root INTO oref."输入的数大于100时会在这里捕获
text = oref->get_text( ).
CONCATENATE 'CATCH cx_root : ’ text INTO text.
ENDTRY.
IF NOT text IS INITIAL.
WRITE / text.
ENDIF.
WRITE: / ‘Final result:’, result.