Skip to Content
Technical Articles
Author's profile photo Nabheet Madan

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.

 

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo monika dhumal
      monika dhumal

       

      Good Information Nabheet!

      Author's profile photo Patrick Van Nierop
      Patrick Van Nierop

      Thanks for sharing this useful bit of information!

      Author's profile photo Sandra Rossi
      Sandra Rossi

      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...

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Call function without exception block still haunts many.. how can i forget:) Now i can recall there are so many..

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      My favorite is SY-SUBRC check inside SELECT... ENDSELECT. 🙂

       

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Good tip, thanks for sharing! I'm wondering - doesn't it show up on the extended check?

       

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks, i just ran it, it does not show up. I think we shall code a custom check which shall take care of all sy-subrc checks for these special statements and that shall be open source so that any one can use it.

      Nabheet

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Good remark. There's a Code Inspector check (Security Checks - Check of SY-SUBRC Handling (class CL_CI_TEST_SYSUBRC)), but limited to Open SQL if I remember well.

      If anyone wants to participate to extend AOC (https://github.com/larshp/abapOpenChecks), where additional checks of SY-SUBRC are done by the check #40 (http://docs.abapopenchecks.org/checks/40/)

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Great, lets get our hands dirty:)

      Author's profile photo Marc Bernard
      Marc Bernard

      Nice blog  Another pitfall is passing sy-subrc as changing parameter  just don’t

      perform x using y changing sy-subrc

       

       

      Author's profile photo Enno Wulff
      Enno Wulff

      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.
      Author's profile photo Mohit Sharma
      Mohit Sharma

      Very useful blog, in fact a tip in particular.

      Much appreciated that you took the time to even think to write this blog.

      Sometime, these small things can really give you a hard time.

      Regards,

      Mohit Sharma