ABAP, How to catch 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.


Read more here: https://stackoverflow.com/questions/64894905/abap-how-to-catch-datref-not-assigned

Content Attribution

This content was originally published by J3W at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: