Try.. Catch .. EndTry.

&---------------------------------------------------------------------
*& 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值