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: 

How to read exception text of a FM

Former Member
0 Kudos
3,750

I have a custom Function Module which has 3 different exceptions in it's exceptions section. I need to get the text for the raised exception in the caller program, how to do that ?

here is a sample call to the FM:

call function 'ZGET_YEAR'

exporting

pernr = l_pernr

importing

year = l_year

exceptions

emp_not_valid = 1

year_not_found = 2

others = 3.

Now, if the emp_not_valid is raised by the FM then I need to get the text stored in that exception.

Appreciate any inputs on it. Thank you.

1 ACCEPTED SOLUTION

uwe_schieferstein
Active Contributor
0 Kudos
1,074

Hello

It seems you are having a wrong perception of exception handling of function modules.

Basically you have to define the messages sent with the exceptions yourself, e.g.:


FUNCTION ZGET_YEAR.

...
  IF ( PERNR is not valid ).
    MESSAGE e398(00) with 'PersNr' pernr 'not valid'
      RAISING emp_not_valid.
  ENDIF.

ENDFUNCTION.

The error message is stored in the corresponding syst variables. In order to get the long version of the error message you can use fm BALW_BAPIRETURN_GET2.


call function 'ZGET_YEAR'
exporting
pernr = l_pernr
importing
year = l_year
exceptions
emp_not_valid = 1
year_not_found = 2
others = 3. 

  IF ( syst-subrc NE 0 ).
      data: ls_return    TYPE bapiret2.

      CLEAR: ls_return.
      " ls_return-id   = syst-msgid.
      " ls_return-type = syst-msgty.
      " ls_return-number = syst.-msgno.
      ls_return-message_v1 = syst-msgv1.
      ls_return-message_v2 = syst-msgv2.
      ls_return-message_v3 = space.
      ls_return-message_v4 = space.
      CALL FUNCTION 'BALW_BAPIRETURN_GET2'
        EXPORTING
          type             = syst-msgty
          cl               = syst-msgid
          number           = syst-msgno
          par1             = ls_return-message_v1
          par2             = ls_return-message_v2
          par3             = ls_return-message_v3
          par4             = ls_return-message_v4
*         LOG_NO           = ' '
*         LOG_MSG_NO       = ' '
*         PARAMETER        = ' '
*         ROW              = 0
*         FIELD            = ' '
        IMPORTING
          return           = ls_return.    

         MESSAGE ls_return-message TYPE ls_return-type.
  ENDIF.

Regards

Uwe

14 REPLIES 14

Former Member
0 Kudos
1,074

Use the standard SY variables for the message data in the calling program

0 Kudos
1,074

Rotem , can you please give me an example?

0 Kudos
1,074

Check FM SWO_TEXT_FUNCTION_EXCEPTION

Regards,

Naimesh Patel

0 Kudos
1,074

Nope, thats not giving me anything back in SHOTTEXT.

0 Kudos
1,074

It is giving me...

I have tried with the

FM: GUI_DOWNLOAD

Exception: ACCESS_DENIED

Language: EN

And I got:

Access to File Denied

Check if there is any typo..

Regards,

Naimesh Patel

0 Kudos
1,074

oh ok that way by passing the exception name? The coding standards here ask to use the exception number for everything. Nobody else has used any exception.

Is there a way to get it done through SY-SUBRC ?

0 Kudos
1,074

Based on the SY-SUBRC, First you have to select the Exception Name from the table FUPARAREF.

Enter:

FUNCNAME = fm

PARAMTYPE = 'X'

PPOSITION = SY-SUBRC.

Get the PARAMETER (which is the exception)

Put it into the FM SWO_TEXT_FUNCTION_EXCEPTION and get the Text.

Regards,

Naimesh Patel

0 Kudos
1,074

In the connection with the Uwe's answer, you can use the MESSAGE .. INTO L_TEXT to avoid using the FM 'BALW_BAPIRETURN_GET2'.


data: l_file type string,
      t_data type standard table of t001,
      lf_message type string.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename                        = l_File
  tables
    data_tab                        = t_data
 EXCEPTIONS
   FILE_WRITE_ERROR                = 1
   NO_BATCH                        = 2
   GUI_REFUSE_FILETRANSFER         = 3
   INVALID_TYPE                    = 4
   NO_AUTHORITY                    = 5
   UNKNOWN_ERROR                   = 6
   HEADER_NOT_ALLOWED              = 7
   SEPARATOR_NOT_ALLOWED           = 8
   FILESIZE_NOT_ALLOWED            = 9
   HEADER_TOO_LONG                 = 10
   DP_ERROR_CREATE                 = 11
   DP_ERROR_SEND                   = 12
   DP_ERROR_WRITE                  = 13
   UNKNOWN_DP_ERROR                = 14
   ACCESS_DENIED                   = 15
   DP_OUT_OF_MEMORY                = 16
   DISK_FULL                       = 17
   DP_TIMEOUT                      = 18
   FILE_NOT_FOUND                  = 19
   DATAPROVIDER_EXCEPTION          = 20
   CONTROL_FLUSH_ERROR             = 21
   OTHERS                          = 22
          .
IF sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         into lF_MESSAGE   " <<
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  message lf_message type sy-msgty .
ENDIF.

Regards,

Naimesh Patel

naimesh_patel
Active Contributor
0 Kudos
1,074

Check table FUNCT.

Pass FUNCNAME = your FM name

PARAMETER = Exception

STEXT = Exception Text

Regards,

Naimesh Patel

0 Kudos
1,074

Thanks. Is there any FM to do this call to table ?

uwe_schieferstein
Active Contributor
0 Kudos
1,075

Hello

It seems you are having a wrong perception of exception handling of function modules.

Basically you have to define the messages sent with the exceptions yourself, e.g.:


FUNCTION ZGET_YEAR.

...
  IF ( PERNR is not valid ).
    MESSAGE e398(00) with 'PersNr' pernr 'not valid'
      RAISING emp_not_valid.
  ENDIF.

ENDFUNCTION.

The error message is stored in the corresponding syst variables. In order to get the long version of the error message you can use fm BALW_BAPIRETURN_GET2.


call function 'ZGET_YEAR'
exporting
pernr = l_pernr
importing
year = l_year
exceptions
emp_not_valid = 1
year_not_found = 2
others = 3. 

  IF ( syst-subrc NE 0 ).
      data: ls_return    TYPE bapiret2.

      CLEAR: ls_return.
      " ls_return-id   = syst-msgid.
      " ls_return-type = syst-msgty.
      " ls_return-number = syst.-msgno.
      ls_return-message_v1 = syst-msgv1.
      ls_return-message_v2 = syst-msgv2.
      ls_return-message_v3 = space.
      ls_return-message_v4 = space.
      CALL FUNCTION 'BALW_BAPIRETURN_GET2'
        EXPORTING
          type             = syst-msgty
          cl               = syst-msgid
          number           = syst-msgno
          par1             = ls_return-message_v1
          par2             = ls_return-message_v2
          par3             = ls_return-message_v3
          par4             = ls_return-message_v4
*         LOG_NO           = ' '
*         LOG_MSG_NO       = ' '
*         PARAMETER        = ' '
*         ROW              = 0
*         FIELD            = ' '
        IMPORTING
          return           = ls_return.    

         MESSAGE ls_return-message TYPE ls_return-type.
  ENDIF.

Regards

Uwe

0 Kudos
1,074

Uwe, you seem to have got my problem. I have never used the exceptions like this before. This is first time I need the text of the exception, which I thought would be a couple of statements away.

Nimaish, your solution did not work. It will not fetch the text stored in Exception. Like when you go to SE37 and define exception for the FM, on exceptions tabm, you give some Short Text and Long Text. I needed that Short Text.

I will be changing the FM to not throw exception. I will pass the text like 'Year not valid' from within the FM as an expotr parameter.

0 Kudos
1,074

At first point of time, I thought you are accustomed with the Exception Handling and you want to read the text of the Exception to avoid givig the speicifc message. So, I have suggested the Function Module Tables which stores the information.

Anyways, I have created a Small Tutorial on : [Function Module Exception Handling|http://help-abap.blogspot.com/2008/10/function-module-exception-handling.html] on my professional blog.

Regards,

Naimesh Patel

Markus_Ebel
Explorer
0 Kudos
1,074

I had the same question today and the blog above is not available anymore.

So I searched myself and found table FUBAREF.

Example:

SELECT SINGLE parameter FROM fupararef
     INTO sy_text
       WHERE funcname = 'EPS2_GET_DIRECTORY_LISTING'
       AND paramtype = 'X'
       AND pposition = sy-subrc.


(I am on a SAP 7.31)