简体   繁体   中英

How to catch the runtime error DATREF_NOT_ASSIGNED?

Is it possible in ABAP to catch DATREF_NOT_ASSIGNED using TRY-CATCH clause?

DATREF_NOT_ASSIGNED is of category ABAP programming error, which means: "Errors in the ABAP program, such as a division by zero or a catchable exception that was not caught."

Problem: modify below code to catch the DATREF_NOT_ASSIGNED:

data gv_i type ref to i.
gv_i->*17.

Simple handling of this error is:

data gv_i type ref to i.
if gv_i is initial.
  gv_i = new #( ).
endif.
gv_i->*17.

The desired solution will use TRY-CATCH or other construct for handling exceptions/errors. Below code does not work:

data gv_i type ref to i.
try.
  gv_i->* = 17.
catch CX_ROOT.
  gv_i = new #( ).
endtry.

You cannot catch DATREF_NOT_ASSIGNED.

The ABAP language documentation for the Dereferencing Operator ->* confirms this:

If the data reference variable contains the null reference, the non-handleable exception DATREF_NOT_ASSIGNED is raised when the dereferencing operator is used.

Use is not bound to check for null references:

data gv_i type ref to i.
if gv_i is not bound.
  gv_i = new #( ).
endif.
gv_i->*17.

You cannot handle such an exception as it is purely a programming error that simply needs to be fixed.

Even CATCH SYSTEM-EXCEPTIONS OTHERS = 8 will not help.

REPORT zzz.

START-OF-SELECTION.
  DATA gv_i TYPE REF TO i.
  CATCH SYSTEM-EXCEPTIONS OTHERS = 8.
    gv_i->* = 17.
  ENDCATCH.
  IF sy-subrc <> 0.
    gv_i = NEW #( ).
  ENDIF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM