捕捉具体的异常比较简单,就说下怎么样捕捉系统未知异常。有了以下处理异常的代码,相信系统出现的任何异常,都不会因为我们的程序而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.
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.
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.
create object ss.
try .
ss->ok( ).
catch cx_root into l_oref.
msg = l_oref->get_text( ).
write:/ msg.
ss->ok( ).
catch cx_root into l_oref.
msg = l_oref->get_text( ).
write:/ msg.
endtry.