ABAP抓取異常的方法:try,endtry
阿新 • • 發佈:2019-02-05
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.
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 = 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
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
ENDTRY.
IF NOT text IS INITIAL.
WRITE / text.
ENDIF.
WRITE: / 'Final result:', result.