abap--关于异常的处理

1、异常分类
从sap 6.10开始,abap的异常分为两类:1)基于异常类的异常,2)非类异常。非类异常又分为系统定义异常(如:被0除异常)和用户自定义异常(用户自定义函数中由exception语句定义,raise语句产生的异常)。

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

2、异常处理语句
基于类异常相关语句:
a)TRY.
  ... guarded section
CATCH cx11 ... cx1n [INTO ex1].
  ... handlers for exceptions cx11 to cx1n
CATCH cx21 ... cx2m [INTO ex2]. 
  ... handlers for exceptions cx21 bis cx2m
  ... other handlers
CLEANUP.
  ... cleanup block
ENDTRY.

b)RAISE EXCEPTION TYPE class.


c)RAISING cx1 ... cxn


非类异常相关语句:
a)catch system-exceptions ARITHMETIC_ERRORS = 4.
....
endcatch.

c) raise (In function or method)

3、异常截获处理方法
Handling exceptions using/with exception classes 截获处理方法
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.

Handling exceptions as catchable runtime errors (向后兼容6.10)
此异常处理sap建议使用try...endtry代替(错误和异常类对应关系参见第5部分)。
data RESULT type I.

catch system-exceptions ARITHMETIC_ERRORS = 4.
  RESULT = 1 / 0.
endcatch.
if SY-SUBRC = 4.
  ...
endif.

4、代码样例
a)RAISING cx1 ... cxn
form adbc_exists_view using view_name type dd25l-viewname
                      changing subrc type sy-subrc
                      raising cx_sql_exception.

 data: stmt type string,
        ref type ref to data,
        stmt_ref type ref to cl_sql_statement,
        res_ref type ref to cl_sql_result_set,
        cnt type sy-tabix.

  subrc = 4.
  create object stmt_ref.
  get reference of view_name into ref.
  stmt_ref->set_param( ref ).
  stmt = 'select count(*) from user_views where view_name = ?'.
  res_ref = stmt_ref->execute_query( stmt ).

* Host-Variable zur Ergebnisaufnahme zuordnen
  get reference of cnt into ref.
  res_ref->set_param( ref ).

  res_ref->next( ).
  if cnt = 1.
    subrc = 0.
  endif.
  res_ref->close( ).

endform.

form exists_view using    view_name type dd25l-viewname
                 changing subrc     type sy-subrc.
  try.
    perform adbc_exists_view(sdb4fora)
            using view_name
            changing subrc.
  catch cx_sql_exception.
    subrc = 8.
  endtry.
endform.
b)基于类的异常代码样例
5、错误与异常类对应关系
Exception group: ARITHMETIC_ERRORS

Class-based Exceptions Definition

*----------------------------------------------------------------------*
*       CLASS class_exception DEFINITION
*----------------------------------------------------------------------*
* All Exception Class must inherit from Class CX_ROOT or its subclass
*----------------
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值