Function Module Exception Handling

By | October 6, 2008 | Exceptions, Function Module | 78,929 | 3

Exception handling is the integral part of the designing the application. By raising the exception, we are providing the oppertunity to the Caller program to handle the exception in its own way.

Visit Raising & Handling Non-class based exceptions to learn more on exception handling and flow control with flow-chart.

Checkout all post related to Exception Raising & handling:

Basics

When we create a reusable component like the Function Module, than we have to consider creating the exception, raising them in the function module and handling them explicitly in the calling program of those function module.

Today, we will see how to raise the exception.
First, we will see how to give the Static text and raise the exception.
Second, we will see how to get the Exception text from the function module repository and use that description in raising the exception.

In the Function module, sometimes we are in the situation when it is not advisable to process further because of, for example, missing data, conversion, etc. At that point of time, we can raise the excpetion. For this, we need to define the Exception under the tab “Exception” in the Function Builder. We have to use the MESSAGE … RAISING EXCEPTION_1 syntax to raise the exception.

In this test application, we have a test function module which will give us the SUM of the numbers provided as input. We want to raise the exception if the number1 (IF_NUM1) is not provided. We can raise the exception by message addition RAISING exception_name.

Static Exception Text

For the first option, here is the code snippet for the Function Module:

FUNCTION ztest_sum_1.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IF_NUM1) TYPE  I OPTIONAL
*"     REFERENCE(IF_NUM2) TYPE  I OPTIONAL
*"  EXPORTING
*"     REFERENCE(EF_SUM) TYPE  I
*"  EXCEPTIONS
*"      NO_NUM_1
*"----------------------------------------------------------------------
*
*
  IF if_num1 IS INITIAL.
    MESSAGE e398(00WITH 'Number 1 has not been supplied' RAISING no_num_1.
    EXIT.
  ENDIF.
*
  ef_sum if_num1 + if_num2.
*
ENDFUNCTION.

Code snippet for the calling program:

*&---------------------------------------------------------------------*
*& Report  ZTEST_SUM
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
*
REPORT  ztest_sum.

START-OF-SELECTION.
*
  DATAlf_num1 TYPE i,
        lf_num2 TYPE i,
        lf_sum  TYPE i.
*
  lf_num2 10.
*
  CALL FUNCTION 'ZTEST_SUM_1'
    EXPORTING
      if_num1  lf_num1
      if_num2  lf_num2
    IMPORTING
      ef_sum   lf_sum
    EXCEPTIONS
      no_num_1 1.
*
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

Dynamic Exception text – retrieved from Exception Repository

For the second option, we need to use the FM SWO_TEXT_FUNCTION_EXCEPTION to get the exception text from the FM repository. Exception text is maintained as like this and we want to read that exception text in our program to raise the exception.

Code snippet to read the exception text from the function module repository.

FUNCTION ztest_sum_1.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IF_NUM1) TYPE  I OPTIONAL
*"     REFERENCE(IF_NUM2) TYPE  I OPTIONAL
*"  EXPORTING
*"     REFERENCE(EF_SUM) TYPE  I
*"  EXCEPTIONS
*"      NO_NUM_1
*"----------------------------------------------------------------------
*
*
  IF if_num1 IS INITIAL.
*----------------
*   Get the Exception Text from the Function Repository
    DATAl_fun TYPE funct-funcname,
          l_exc TYPE funct-parameter,
          l_txt TYPE swotlq-shorttext.
*
    l_fun 'ZTEST_SUM_1'.
    l_exc 'NO_NUM_1'.
*
*   text of the Exception
    CALL FUNCTION 'SWO_TEXT_FUNCTION_EXCEPTION'
      EXPORTING
        language  sy-langu
        function  l_fun
        exception l_exc
      IMPORTING
        shorttext l_txt.
*
*   Raise the exception and exit
    MESSAGE e398(00WITH l_txt RAISING no_num_1"(l_exc).
    EXIT.
*
  ENDIF.
*-------------------
*
  ef_sum if_num1 + if_num2.
*
ENDFUNCTION.

Check the help from SAP: Message Handling in Function Modules

Further Reading

Checkout all post related to Exception Raising & handling:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

3 Comments

  • I tried with ztest_sum_1 and did not works. the funct table is empty (the short text of the exception is filled)

    tks

  • Anonymous

    i try this and it works.. you need to define the interface in the function module

  • […] continuation to one of my old post Function Module Exception Handling, today we’ll check some more […]

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.