Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

easy way to get the exception text returned by FM (without case/IF)

former_member188019
Active Participant
0 Kudos

Hi All,

it is basic thing, but want to generalize my code, so asking,

    DATA lv_str TYPE string.

      DATA: lt_p1001 TYPE TABLE OF p1001.

      CALL FUNCTION 'RH_READ_INFTY'

        EXPORTING

          authority            = 'DISP'

          with_stru_auth       = 'X'

          plvar                = '01'

          otype                = 'S'

          objid                = '1234678'

          infty                = '1001'

          istat                = '1'

        TABLES

          innnn                = lt_p1001

        EXCEPTIONS

          all_infty_with_subty = 1

          nothing_found        = 2

          no_objects           = 3

          wrong_condition      = 4

          wrong_parameters     = 5

          OTHERS               = 6.

I want to get the codes e.g all_infty_with_subty or nothing_found, not just sy-subrc 1 or 2 at runtime, so that i can pass the code

Currently i am getting with case statements like below.

      IF sy-subrc <> 0.

*      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

*      INTO mtext WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

        CASE sy-subrc.

          WHEN 1.

            lv_str = 'all_infty_with_subty'.

          WHEN 2.

            lv_str = 'nothing_found'.

          WHEN 3.

            lv_str = 'no_objects'.

          WHEN 4.

            lv_str = 'wrong_condition'.

          WHEN 5.

            lv_str = 'wrong_parameters'.

          WHEN 6.

            lv_str = 'others'.

          WHEN OTHERS.

        ENDCASE.

      ENDIF.

 

In our project, there are lot of FMs, which return exceptions, but i am looking for generic way to get the exception code text based on sy-subrc.

i tried using the FMs  FORMAT_MESSAGE and BAPI_MESSAGE_GETDETAIL, but they are not giving the exception code text.

thanks in advance.

Madhu_1980

15 REPLIES 15

raymond_giuseppi
Active Contributor
0 Kudos

You can get the list of exception Codes and Texts in table FUNCT (FM signature, select language and kind = 'X')

But insure to respect the order of exceptions in your code (Alphabetical on code?)

Regards,

Raymond

0 Kudos

Hi Madhu

In addition to suggestion, fetch all stext in an internal table for a particular function module and read that internal table at index sy-subrc to get the correct stext irrespective of alphabetical order.

Thanks

Former Member
0 Kudos

Hi Madhu,

Use the FM -FUNCTION_IMPORT_INTERFACE and pass your FM name.

The above FM will return you all the import,exporting,tables and even the list of exceptions(along with the text).

Let me know if you need any more inputs to it.

Regards,

Gupta

0 Kudos

Hi Madhu,

There is no other way for the same. What you have done is the only option you have.

Regards,

Rakesh

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

You can try using this FM "SWO_TEXT_FUNCTION_EXCEPTION" It returns exception text when you pass function module name and exception name as:

Pass lv_str value to lv_exec which contains exception name

    CALL FUNCTION 'SWO_TEXT_FUNCTION_EXCEPTION'

      EXPORTING

        language  = sy-langu

        function  =  'RH_READ_INFTY'          “Function module name

        exception = lv_exec                              “Exception name like ALL_INFTY_WITH_SUBTY

      IMPORTING

        shorttext = “text.

0 Kudos

Hi Neha,

It its very handy way to get the exception text.

But we have to still use the case statement as Madhu has done.

Any way the FM is very useful.

0 Kudos

Yes that case is required. as exception name we are capturing in the CASE statement. Just gave rest of the logic.

Thanks,

Neha

former_member185054
Active Participant
0 Kudos

if sy-subrc ne 0. 

CALL FUNCTION 'SWO_TEXT_FUNCTION_EXCEPTION'

    EXPORTING

     LANGUAGE        = SY-LANGU

      function        = RH_READ_INFTY

      exception       =

   IMPORTING

   SHORTTEXT       = lv_str

endif.

Anyhow inside this Fm they are using Funct table only.

SELECT * FROM FUNCT INTO TABLE L_FUNC_EXCEPTIONS

      WHERE FUNCNAME    = FUNCTION

       AND  SPRAS       = LANGUAGE

       AND  KIND        = 'X'

       AND  VERSION     = '0001'.

Regards,

sampath kumar

raymond_giuseppi
Active Contributor
0 Kudos

And now you just have to build a pattern to replace the standard CALL FUNCTION pattern of Abap Editor, with the full code pre-packaged

Regards,

Raymond

Former Member
0 Kudos

I don't think this is possible. After the execution of the FM, you don't know the exception that was raised; you only know sy-subrc. And you assign that to the exception when calling the FM.

FM SWO_TEXT_FUNCTION_EXCEPTION just returns the exception short text if you already know the exception.

Which you don't.

Rob

former_member185054
Active Participant
0 Kudos

Hi,

yeah i think , only way is select STEXT from FUNCT where spars = 'EN' and kind = 'X' and version = '0001'.

into  table lv_str1.

read table lv_str1 into lv_str insex sy-subrc.

yeah you are right Raymond Giuseppi,Always Learner

regards,

sampath kumar

raymond_giuseppi
Active Contributor
0 Kudos

A little clarification to wind up the discussion : You have to join text table FUNCT with table FUPARAREF to get actual exceptions of FM and their position (FUPARAREF-PPOSITION) so the defaulted subrc.

Regards,

Raymond

0 Kudos

thanks, it might help.

      IF sy-subrc <> 0.

          lv_str = 'OTHERS' "as it is not there in table.

                   select single parameter from FUPARAREF into lv_str where

               funcname = 'RH_READ_INFTY'

               and paramtype = 'X'

               and sy-subrc = pposition.

      ENDIF.


0 Kudos

I think you could use the FM which Gopal Gupta suggested, FUNCTION_IMPORT_INTERFACE.

eg, here is a prototype using a date conversion FM and catching the exception generically...

REPORT Ztest_exceptions.

DATA lv_str TYPE string.

data:

my_fm type RS38L_FNAM,

my_EXCEPTION_LIST type standard table of RSEXC,

my_EXCEPTION type RSEXC,

my_EXPORT_PARAMETER type standard table of  RSEXP,

my_IMPORT_PARAMETER type standard table of  RSIMP,

my_TABLES_PARAMETER type standard table of RSTBL,

my_in_date type TUMLS_DATE value '32.01.2016',

my_out_date type TUMLS_DATE,

my_subrc type sysubrc.

my_fm = '/SAPDMC/LSM_DATE_CONVERT'.

* call the FM...

CALL FUNCTION my_fm

  EXPORTING

    DATE_IN                   = my_in_date

    DATE_FORMAT_IN            = 'DDMY'

*   TO_OUTPUT_FORMAT          = 'X'

*   TO_INTERNAL_FORMAT        = ' '

IMPORTING

   DATE_OUT                  = my_out_date

EXCEPTIONS

   ILLEGAL_DATE              = 1

   ILLEGAL_DATE_FORMAT       = 2

   NO_USER_DATE_FORMAT       = 3

   OTHERS                    = 4

          .

* save the subrc

my_subrc = sy-subrc.

* get the exception list for the FM

CALL FUNCTION 'FUNCTION_IMPORT_INTERFACE'

  EXPORTING

    FUNCNAME                      = my_fm

*   INACTIVE_VERSION              = ' '

*   WITH_ENHANCEMENTS             = 'X'

*   IGNORE_SWITCHES               = ' '

* IMPORTING

*   GLOBAL_FLAG                   =

*   REMOTE_CALL                   =

*   UPDATE_TASK                   =

*   EXCEPTION_CLASSES             =

*   REMOTE_BASXML_SUPPORTED       =

  TABLES

    EXCEPTION_LIST                = my_EXCEPTION_LIST

    EXPORT_PARAMETER              = my_EXPORT_PARAMETER

    IMPORT_PARAMETER              = my_IMPORT_PARAMETER

*   CHANGING_PARAMETER            =

    TABLES_PARAMETER              = my_TABLES_PARAMETER

*   P_DOCU                        =

*   ENHA_EXP_PARAMETER            =

*   ENHA_IMP_PARAMETER            =

*   ENHA_CHA_PARAMETER            =

*   ENHA_TBL_PARAMETER            =

*   ENHA_DOCU                     =

EXCEPTIONS

   ERROR_MESSAGE                 = 1

   FUNCTION_NOT_FOUND            = 2

   INVALID_NAME                  = 3

   OTHERS                        = 4

          .

IF MY_SUBRC <> 0.

* Implement suitable error handling here

  read table my_EXCEPTION_LIST index my_subrc into my_exception.

  concatenate my_in_date 'raises exception' my_exception-exception

   into lv_str separated by space.

  MESSAGE lv_str TYPE 'S'.

ENDIF.