EXCEPTIONS——异常处理总结

一、ABAP异常分类
ABAP异常分为两类:1、基于异常类的异常,2、非类异常(2.1、系统定义的异常(如算术异常“cx_sy_arithmetic_error” 0被除等),2.2、用户自定义的异常(自建函数中由exception语句定义,raise语句产生的异常))。

PS:异常有的是可以截获做相应处理,有的异常为不可截获的错误异常,系统将直接产生错误,并停止执行程序。

二、基于类的异常捕获

2.1、基于类的异常是以异常类实例方式来实现,异常类是系统预先定义好的全局类(PACKAGE:S_ABAP_EXCEPTIONS)或者是由用户自定义的局部或全局的类。
EXCEPTIONS——异常处理总结_第1张图片
2.2 基本语法

DATA myref TYPE REF TO cx_sy_arithmetic_error.
DATA err_text TYPE string.
DATA result TYPE i.
TRY.
    result = 1 / 0.
CATCH cx_sy_arithmetic_error INTO myref.
    err_text = myref->get_text( ).
ENDTRY.

2.3、程序实例

*&---------------------------------------------------------------------*
*& Report  ZEXCEPTION_DEMO1
*&
*&---------------------------------------------------------------------*
*& 6.1版本以后,TRY…ENDTRY结构:
*&TRY.
*&    [try_block]
*&CATCH cx_class1 cx_class2 ... [INTO oref].
*&    [catch_block]
*&    ...
*&[CLEANUP [INTO oref].
*&    [cleanup_block]]
*&ENDTRY.
*&CLEARUP是在所在的TRY CATCH发生了异常,但在本TRY CATCH中无法捕获,异常再次向上递交给上层TRY CATCH前被执行的,它不同于Java中的finally
*&---------------------------------------------------------------------*

report zexception_demo1.

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. "RAISE EXCEPTION手动触发异常
      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时会在这里捕获
        catch cx_sy_arithmetic_error 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时会在这里捕获
    catch cx_demo_abs_too_large 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:' NO-GAP, result NO-GAP.

三、函数异常的定义、抛出与处理
2.1 示例函数 ‘READ_TEXT’
(1)定义
EXCEPTIONS——异常处理总结_第2张图片
(2)抛出
EXCEPTIONS——异常处理总结_第3张图片
(3)处理
EXCEPTIONS——异常处理总结_第4张图片
2.2 示例代码
&---------------------------------------------------------------------
*& Report ZEXCEPTION_DEMO2
*& 函数异常的定义、抛出、与处理
&---------------------------------------------------------------------
*&
*&
&---------------------------------------------------------------------

report zexception_demo2.
data:ltxt_lines type table of tline.
call function 'READ_TEXT'
  exporting
    id                      = 'GRUN'
    language                = sy-langu
    name                    = 'WANGMQ'
    object                  = 'MATERRIAL'
  tables
    lines                   = ltxt_lines
  exceptions
    id                      = 1
    language                = 2
    name                    = 3
    not_found               = 4
    object                  = 5
    reference_check         = 6
    wrong_access_to_archive = 7
    others                  = 8.
case sy-subrc.
  when 1. message 'Text ID invalid' type 'S' display like 'E'.
  when 2. message 'Invalid language' type 'S' display like 'E'.
  when 3. message 'Invalid text name' type 'S' display like 'E'.
  when 4. message 'Text not found' type 'S' display like 'E'.
  when 5. message 'Invalid text object' type 'S' display like 'E'.
  when 6. message 'Reference chain interrupted' type 'S' display like 'E'.
  when 7. message 'Archive handle invalid for access' type 'S' display like 'E'.
  when 8. message 'others' type 'S' display like 'E'.
endcase.

你可能感兴趣的:(ABAP:异常处理)