Skip to Content
Technical Articles

SY-SUBRC Check misadventures, Do not use for the heck of it!

Background  

This morning me and my colleague Nikhil Maradi were checking odd behavior in a productive ABAP code, infact we were working on an incident. The problem was related to a SY-SUBRC field value being set as 0 in one system and 4 in another system for the same statement, sounds interesting right read on. This small blog will talk about this weird SY-SUBRC behavior and a very important learning for us.

Problem statement

Our production code is using CREATE DATA statement to create types based on variable.The work area has value of KNKK in it. As can be seen the SY-SUBRC value is being returned 4 in one SAP instance and 0 in another SAP instance.

Our Analysis and Solution

So the first step in finding the root cause of the issue was to check ABAP help does CREATE DATA statement sets any SY-SUBRC value if it fails. As always on searching the help here i could not find any note about the same also shown in screen shot.

On looking at the example as shown below we could see SAP example code is neither checking the SY-SUBRC after the create statement nor catching any exceptions also. We missed checking the executable example at this point else would have found something which we found later so read on!

So for the time being we assumed that somehow we don’t need to check SY-SUBRC neither catch any exceptions and our code will work. But still deep down our heart we were not convinced with the line of reasoning. We decided to dig more into ABAP help and read around as shown below.

This is where we found a  meaningful note we found which actually helped in finding the justifications for our assumptions. In the parent help it was clearly mentioned that CREATE DATA will not set any SY-SUBRC

In addition we shall catch the exceptions if we want to trouble shoot.

After understanding the detail the modified sample code looks something like below and same concept is incorporated in the productive code after removing the SY-SUBRC check.

DATA:dref      TYPE REF TO data,
     tablename TYPE tabname16 VALUE 'ZKNKK'.
TRY.
    CREATE DATA dref TYPE (tablename).
  CATCH cx_sy_create_data_error.
    cl_demo_output=>display( |Error creating { tablename }| ).
ENDTRY.

In case a type does not exist the error is also displayed.

Learning for all ABAPers

As an ABAPer we tend to put SY-SUBRC check in most of the places by assuming that this field will be set, which is not true as seen above. So takeaway for us is as follow

  • DO NOT ASSUME, refer ABAP help documentation and exmaple in detail.
  • A way must exist determine whether the statement worked or not via SY-SUBRC,Catching Exceptions etc.

It will be great if our fellow community members can share some of such crazy leanrings in the comments below.

 

11 Comments
You must be Logged on to comment or reply to a post.
  • People frequently and erroneously check SY-SUBRC after:

    • SUBMIT (except if VIA JOB is used)
    • CALL FUNCTION where the EXCEPTIONS block is absent (it’s always 0)

    There is also a bunch to say about checking SY-SUBRC after Open SQL statements, or not, or changing the lines of code so that SY-SUBRC can be checked efficiently to avoid useless dummy SY-SUBRC checks…

  • It’s also not clever to provide SY-SUBRC in an exception…

    REPORT.
    
    CLASS lx_failure DEFINITION INHERITING FROM cx_static_check.
      PUBLIC SECTION.
        METHODS constructor IMPORTING subrc TYPE sysubrc.
        DATA mv_subrc TYPE sysubrc.
    ENDCLASS.
    CLASS lx_failure IMPLEMENTATION.
      METHOD constructor.
        super->constructor( ).
        mv_subrc = subrc.
      ENDMETHOD.
    ENDCLASS.
    
    
    
    CLASS main DEFINITION.
      PUBLIC SECTION.
        METHODS test_sy_subrc  RAISING lx_failure.
        METHODS test_var_subrc RAISING lx_failure.
    ENDCLASS.
    
    CLASS main IMPLEMENTATION.
      METHOD test_sy_subrc.
        sy-subrc = 6.
        RAISE EXCEPTION TYPE lx_failure EXPORTING subrc = sy-subrc.
      ENDMETHOD.
      METHOD test_var_subrc.
        data(subrc) = 6.
        RAISE EXCEPTION TYPE lx_failure EXPORTING subrc = subrc.
      ENDMETHOD.
    ENDCLASS.
    
    START-OF-SELECTION.
      TRY.
          NEW main( )->test_sy_subrc( ).
        CATCH lx_failure INTO DATA(failed_sy).
          MESSAGE |sy-subrc: { failed_sy->mv_subrc }| TYPE 'I'.
      ENDTRY.
    
      TRY.
          NEW main( )->test_var_subrc( ).
        CATCH lx_failure INTO DATA(failed_var).
          MESSAGE |var subrc: { failed_var->mv_subrc }| TYPE 'I'.
      ENDTRY.