ABAP中的异常处理 - TRY CATCH的使用实例

在平时的ABAP开发中,需要捕获的异常通常为两种,一种是执行SQL,比如主键重复,INSERT语句字段类型不匹配等。还有就是RFC的通信错误,比如不能进行远程连接等。通常可以这么处理:
1.数据库异常:
  DATA: lcx_error TYPE REF TO cx_root. "cx_sy_sql_error . "cx_sy_open_sql_db.
  DATA i_tab LIKE TABLE OF spfli WITH HEADER LINE.
  DATA err_text TYPE c LENGTH 1000.
  
   TRY.
   INSERT spfli FROM TABLE i_tab.
   CATCH cx_sy_open_sql_db INTO lcx_error.
   err_text = lcx_error->get_text( ). "得到错误信息
   sy-subrc = 4. "改变系统变量
   ENDTRY.
  
   IF sy-subrc <> 0.
   WRITE :/ '数据更新失败!'.
   WRITE err_text.
   ROLLBACK WORK.
   ELSE.
   WRITE :/ '数据更新成功!'.
   COMMIT WORK.
   ENDIF.
2.RFC异常:
TRY.
CALL FUNCTION 'Z_DAQ_CALL_JCO'
DESTINATION 'ZJCOSERVER_DAQ'
EXPORTING
pv_empid = l_empid
IMPORTING
pv_rlt = l_rlt
TABLES
it_spfli = i_tab
EXCEPTIONS
system_failure = 1 MESSAGE err_text
communication_failure = 2 MESSAGE err_text.
ENDTRY.
IF SY-SUBRC <> 0.
WRITE: / '调用***失败!' , err_text.
ENDIF.

-------------------------------------
详细的信息可以参加下面的内容:
http://blog.csdn.net/CompassButton/archive/2007/04/03/1550818.aspx
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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值