二维码

ABAP开发中异常处理使用方法及常见案例

Twilight发表于 2015-04-15 10:18Twilight 最后回复于 2015-04-15 10:18 [复制链接] 3757 0

几乎所有的语言都有异常处理,ABAP也不例外。合理的使用异常处理能大大健壮我们的程序,例如除运算时分母为零,或者除数为非数值,那么程序直接Dump,这些情况下我们使用ABAP异常捕获就能捕获到错误消息,避免出现dump。

ABAP异常是基于类实现的,下面列举了捕获异常的三种使用方式:
1、最常见方式,推荐
  1. DATA: lcx_error TYPE REF TO cx_sy_arithmetic_error.
  2. DATA: lv_err_text TYPE string.
  3. DATA: result TYPE i.
  4. TRY.
  5.     result = 1/0.
  6.   CATCH cx_sy_arithmetic_error INTO lcx_error.
  7.     lv_err_text = lcx_error->get_text( ).
  8.     MESSAGE e001(00) WITH '除运算错误:' err_text.
  9. ENDTRY.
复制代码
多种类型异常处理:
  1. DATA: lcx_error_zero TYPE REF TO cx_sy_zerodivide.
  2. DATA: lcx_error_numt TYPE REF TO cx_sy_arithmetic_error.
  3. DATA: lcx_error_root TYPE REF TO cx_root.
  4. DATA: lv_err_text TYPE string.
  5. DATA: result TYPE i.
  6. TRY.
  7.     result = 'saqw' / 0.
  8.   CATCH cx_sy_zerodivide INTO lcx_error_zero.
  9.     lv_err_text = lcx_error_zero->get_text( ).
  10.     MESSAGE e001(00) WITH '除运算错误:' lv_err_text.
  11.   CATCH cx_sy_arithmetic_error INTO lcx_error_numt.
  12.     lv_err_text = lcx_error_numt->get_text( ).
  13.     MESSAGE e001(00) WITH '除运算错误:' lv_err_text.
  14.   CATCH cx_root INTO lcx_error_root.
  15.     lv_err_text = lcx_error_root->get_text( ).
  16.   CLEANUP.
  17.     CLEAR result.
  18. ENDTRY.
复制代码
异常处理顺序原则:从子类到父类
CLEANUP 异常处理最后执行,一般用来关闭文件连接、清空变量、释放内存等

2、老系统若不支持上面的方式,可以采用下面方式
  1. DATA result TYPE i.
  2. CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4.
  3.   result = 1 / 0.
  4. ENDCATCH.
  5. IF sy-subrc = 4.
  6.   MESSAGE e001(00) WITH '除运算错误:分母为零'.
  7. ENDIF.
复制代码
3、RFC异常捕获
  1. TRY.
  2.     CALL FUNCTION 'Z_DAQ_CALL_JCO'
  3.       DESTINATION 'ZJCOSERVER_DAQ'
  4.       EXPORTING
  5.         pv_empid              = l_empid
  6.       IMPORTING
  7.         pv_rlt                = l_rlt
  8.       TABLES
  9.         it_spfli              = i_tab
  10.       EXCEPTIONS
  11.         system_failure        = 1 MESSAGE lv_err_text
  12.         communication_failure = 2 MESSAGE lv_err_text.
  13. ENDTRY.
  14. IF sy-subrc <> 0.
  15.   WRITE: / '调用***失败!' , lv_err_text.
  16. ENDIF.
复制代码
注意:只能在远程调用是捕获异常,本地调用是不能捕获到异常的,异常原因就2种:
SYSTEM_FAILURE
  1. This exception reports all failures and system problems on the remote machine.
复制代码
COMMUNICATION_FAILURE
  1. This exception is raised when a connection or communications failure occurs. It does not report system problems (for example, abnormal termination) that occur on the remote machine.
复制代码

在不同常见使用响应的捕获异常类是关键,下面列举了所有类异常类树:
  1. CX_SY_ROOT
  2.   |
  3.   |--CX_STATIC_CHECK
  4.   |
  5.   |--CX_DYNAMIC_CHECK
  6.   |    |
  7.   |    |--CX_SY_ARITHMETIC_ERROR
  8.   |    |    |
  9.   |    |    |--CX_SY_ZERODIVIDE
  10.   |    |    |
  11.   |    |    |--CX_SY_ARITHMETIC_OVERFLOW
  12.   |    |    |
  13.   |    |    |--CX_SY_ARG_OUT_OF_DOMAIN
  14.   |    |    |
  15.   |    |    |--CX_SY_PRECISION_LOSS
  16.   |    |
  17.   |    |--CX_SY_ASSIGN_ERROR
  18.   |    |    |
  19.   |    |    |--CX_SY_ASSIGN_CAST_ERROR
  20.   |    |    |    |
  21.   |    |    |    |--CX_SY_ASSIGN_CAST_ILLEGAL_CAST
  22.   |    |    |    |
  23.   |    |    |    |--CX_SY_ASSIGN_CAST_UNKNOWN_TYPE
  24.   |    |    |
  25.   |    |    |--CX_SY_ASSIGN_OUT_OF_RANGE
  26.   |    |
  27.   |    |--CX_SY_CODEPAGE_CONVERTER_INIT
  28.   |    |
  29.   |    |--CX_SY_CONVERSION_ERROR
  30.   |    |    |
  31.   |    |    |--CX_SY_CONVERSION_OVERFLOW
  32.   |    |    |
  33.   |    |    |--CX_SY_CONVERSION_NO_NUMBER
  34.   |    |    |
  35.   |    |    |--CX_SY_CONVERSION_CODEPAGE
  36.   |    |    |
  37.   |    |    |--CX_SY_CONVERSION_BASE64
  38.   |    |    |
  39.   |    |    |--CX_SY_CONV_ILLEGAL_DATE_TIME
  40.   |    |
  41.   |    |--CX_SY_CREATE_ERROR
  42.   |    |    |
  43.   |    |    |--CX_SY_CREATE_OBJECT_ERROR
  44.   |    |    |
  45.   |    |    |--CX_SY_CREATE_DATA_ERROR
  46.   |    |
  47.   |    |--CX_SY_DATA_ACCESS_ERROR
  48.   |    |    |
  49.   |    |    |--CX_SY_RANGE_OUT_OF_BOUNDS
  50.   |    |    |
  51.   |    |    |--CX_SY_OFFSET_NOT_ALLOWED
  52.   |    |
  53.   |    |--CX_SY_DYN_CALL_ERROR
  54.   |    |    |
  55.   |    |    |--CX_SY_DYN_CALL_ILLEGAL_CLASS
  56.   |    |    |
  57.   |    |    |--CX_SY_DYN_CALL_ILLEGAL_FUNC
  58.   |    |    |
  59.   |    |    |--CX_SY_DYN_CALL_ILLEGAL_METHOD
  60.   |    |    |
  61.   |    |    |--CX_SY_DYN_CALL_PARAMETER_ERROR
  62.   |    |         |
  63.   |    |         |--CX_SY_DYN_CALL_EXCP_NOT_FOUND
  64.   |    |         |
  65.   |    |         |--CX_SY_DYN_CALL_ILLEGAL_TYPE
  66.   |    |         |
  67.   |    |         |--CX_SY_DYN_CALL_PARAM_MISSING
  68.   |    |         |
  69.   |    |         |--CX_SY_DYN_CALL_PARAM_NOT_FOUND
  70.   |    |
  71.   |    |--CX_SY_FILE_ACCESS_ERROR
  72.   |    |    |
  73.   |    |    |--CX_SY_FILE_AUTHORITY
  74.   |    |    |
  75.   |    |    |--CX_SY_FILE_CLOSE
  76.   |    |    |
  77.   |    |    |--CX_SY_FILE_IO
  78.   |    |    |
  79.   |    |    |--CX_SY_FILE_POSITION
  80.   |    |    |
  81.   |    |    |--CX_SY_FILE_OPEN_MODE
  82.   |    |    |
  83.   |    |    |--CX_SY_FILE_OPEN
  84.   |    |
  85.   |    |--CX_SY_GEN_SOURCE_TOO_WIDE
  86.   |    |
  87.   |    |--CX_SY_IMPORT_MISMATCH_ERROR
  88.   |    |
  89.   |    |--CX_SY_MOVE_CAST_ERROR
复制代码

异常捕捉实战应用案例:ABAP项目中常见异常处理场景汇总

回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表