Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALL METHOD with dynamic method name, RuntimeException

Tags:

abap

I try to demonstrate a CALL METHOD statement with dynamic method name on a 7.40 system. I use the following test code and get an ABAP Runtime Error in line 27. The Error Analysis in the Description of Exception states ... in the class LCL, the method "m" could not be found. But the standalone method call succeeds 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
 
like image 546
Ulrich Scholz Avatar asked Mar 14 '26 23:03

Ulrich Scholz


1 Answers

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.
like image 133
József Szikszai Avatar answered Mar 16 '26 21:03

József Szikszai