带有动态方法名称的CALLMETHOD,RuntimeException
我尝试CALL METHOD在 7.40 系统上演示带有动态方法名称的语句。我使用以下测试代码并在第 27 行得到 ABAP 运行时错误。 异常状态描述中的错误分析... in the class LCL, the method "m" could not be found. 但是独立方法调用成功调用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
回答
In the background all method names are uppercase, so you have to call the method like this:
DATA name TYPE c VALUE 'M'.
On the other hand you can catch this exception, so the program won't dump, even if the method does not exist:
TRY.
CALL METHOD oref->(name).
CATCH cx_sy_dyn_call_illegal_method
INTO DATA(lx_illegal_method).
"handle illegal method call
ENDTRY.