Chinaunix首页 | 论坛 | 博客
  • 博客访问: 174504
  • 博文数量: 31
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 585
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-17 23:11
文章分类

全部博文(31)

文章存档

2011年(1)

2009年(18)

2008年(12)

我的朋友

分类:

2008-03-27 20:47:26

捕捉具体的异常比较简单,就说下怎么样捕捉系统未知异常。有了以下处理异常的代码,相信系统出现的任何异常,都不会因为我们的程序而dump. 我们用cx_root(RFC需要用exceptions).
经过测试,db错误,不仅是open-sql,还有native-sql产生的错误都可以处理,还有function参数类型不匹配,function名字错误等都可以处理。如果谁发现有不能处理的异常,请留言。
 
REPORT  z_victor_pan                           .
data: name type string.
DATA: l_oref TYPE REF TO cx_root,
      exception_msg(1000),
      msg(1000).
class test definition.
public section. methods ok.
endclass.
class test implementation.
method ok.
data: a1 type i.
try.
a1 = 100 / 0.
catch cx_root  into l_oref.
msg = l_oref->get_text( ) .
write:/ msg.
endtry.
endmethod.
endclass.
start-of-selection.
 
try.
  call function 'ZPZTEST_RFC1' destination 'RGSCLNT301'
    IMPORTING
      name                  = name
    EXCEPTIONS
      SYSTEM_FAILURE        = 1  MESSAGE exception_msg
      COMMUNICATION_FAILURE = 2  MESSAGE exception_msg.
  write:/ exception_msg.
  clear: exception_msg, msg.
endtry.

try.
    call function 'ZPZTE'.
*     EXCEPTIONS
*     SYSTEM_FAILURE = 1 MESSAGE msg.
*catch CX_SY_DYN_CALL_ILLEGAL_FUNC into l_oref.
  catch cx_root  into l_oref.
    exception_msg = l_oref->get_text( ).
    write:/ msg.
    clear msg.
endtry.

try.
    name = 100 / 0.
  catch cx_root into l_oref.
    msg =  l_oref->get_text( ).
    write:/ msg.
endtry.
clear msg.
try.
name = 100 / 0.
call function 'ZPZTE'.
call method CL_GUI_FRONTEND_SERVICES=>(msg).
catch cx_root into l_oref.
 msg =  l_oref->get_text( ).
    write:/ msg.
endtry.
data:  ss type ref to test.
create object ss.
try .
ss->ok( ).
catch cx_root into l_oref.
msg =  l_oref->get_text( ).
 write:/ msg.
endtry.
阅读(2525) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

miracleye2008-03-28 12:50:21

mvc异常亦可以用cx_root捕获 。如有什么其它异常无法捕捉,可以继续发贴讨论。cx_root是cx_***的最底层类,一层一层继承而来,包括bsp. 例如CX_BSP_T100_EXCEPTION,继承CX_BSP_EXCEPTION,而CX_BSP_EXCEPTION继承CX_NO_CHECK,而CX_NO_CHECK继承CX_ROOT。

chinaunix网友2008-03-28 09:47:07

这么快就搞定了.来凑个热闹. 下面的例子可以捕捉普通异常: 1.Error class 的使用例子: CATCH SYSTEM-EXCEPTIONS convt_no_number = 1. wa_dept-budget = l_budget->value. ENDCATCH. IF sy-subrc = 1. p_message = '*预算应该是数值类型'. EXIT. ENDIF. DATA: result TYPE i, number TYPE i. CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4 OTHERS = 8. ... result = 1 / number. ... ENDCATCH. IF sy-subrc <> 0. ... ENDIF. 2. Catch SYSTE