使用动态方法名称调用方法,RuntimeException
CALL METHOD with dynamic method name, RuntimeException
我尝试在 7.40 系统上演示带有动态方法名称的 CALL METHOD
语句。我使用下面的测试代码,在第27行得到一个ABAP Runtime Error。Exception states中的Error Analysis ... in the class LCL, the method "m" could not be found.
But standalone method call successful in calling m
.
REPORT ZUTEST10.
CLASS lcl DEFINITION.
PUBLIC SECTION.
METHODS m.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
METHOD m.
write / 'success'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA oref TYPE REF TO lcl.
CREATE OBJECT oref.
oref->m( ). " works fine
DATA name TYPE c VALUE 'm'.
CALL METHOD oref->(name). " <-- Runtime Error
在后台所有方法名都是大写的,所以你必须这样调用方法:
DATA name TYPE c VALUE 'M'.
另一方面,您可以捕获此异常,因此即使该方法不存在,程序也不会转储:
TRY.
CALL METHOD oref->(name).
CATCH cx_sy_dyn_call_illegal_method
INTO DATA(lx_illegal_method).
"handle illegal method call
ENDTRY.
我尝试在 7.40 系统上演示带有动态方法名称的 CALL METHOD
语句。我使用下面的测试代码,在第27行得到一个ABAP Runtime Error。Exception states中的Error Analysis ... in the class LCL, the method "m" could not be found.
But standalone method call successful in calling m
.
REPORT ZUTEST10.
CLASS lcl DEFINITION.
PUBLIC SECTION.
METHODS m.
ENDCLASS.
CLASS lcl IMPLEMENTATION.
METHOD m.
write / 'success'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA oref TYPE REF TO lcl.
CREATE OBJECT oref.
oref->m( ). " works fine
DATA name TYPE c VALUE 'm'.
CALL METHOD oref->(name). " <-- Runtime Error
在后台所有方法名都是大写的,所以你必须这样调用方法:
DATA name TYPE c VALUE 'M'.
另一方面,您可以捕获此异常,因此即使该方法不存在,程序也不会转储:
TRY.
CALL METHOD oref->(name).
CATCH cx_sy_dyn_call_illegal_method
INTO DATA(lx_illegal_method).
"handle illegal method call
ENDTRY.