ABAP – catch error messages from function modules

It it easy to catch named exceptions thrown by a function module in section EXCEPTIONS when calling such FM. But what if an error message is thrown inside the FM without specifying the exception name?

Most of the error messages thrown within a FM are exceptions done by RAISE or MESSAGE RAISING together with the exception name. Such messages can be caught by explicitly mentioning the exception name in the EXCEPTIONS section of the function module call or by using the “OTHERS” which collects all remaining exception names.

But what if there’s a message thrown within a function module where the exception name is not specified?

You can use addition error_message = n_error

If the predefined exception error_message is specified after EXCEPTIONS, all messages

  • that are sent using the statement MESSAGE without the addition RAISING,
  • that are sent using the statement MESSAGE RAISING because no return code is assigned to them,
  • that are sent by the ABAP runtime environment

during function module processing are affected as follows:

  • Messages of the type S, I, or W are not sent but are flagged in the log in background processing.
  • Messages of the type E and A raise the exception error_message and set sy-subrc to n_error. The message class, message type, message number, and the contents of possible placeholders for the MESSAGE statement are in the fields sy-msgid, sy-msgno, sy-msgty, and sy-msgv1, … sy-msgv4. With messages of the type A, the statement ROLLBACK WORK is also executed explicitly.
  • Messages of the type X are not affected – they cause a program termination with a short dump.

You can test and verify it using this quick example code:

Here comes the function module

FUNCTION z_mvoy_error.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_IS_NORMAL_EXCEPTION) TYPE  FLAG
*"  EXCEPTIONS
*"      NORMAL_EXCEPTION
*"----------------------------------------------------------------------
  IF iv_is_normal_exception = 'X'.

    MESSAGE 'Cool, you can easily handle me...' TYPE 'E'
      RAISING normal_exception.
  ELSE.
    MESSAGE 'Ooops, who can handle me???'       TYPE 'E'.
  ENDIF.

ENDFUNCTION.

…and here’s the report that calls such function module

SELECTION-SCREEN BEGIN OF BLOCK bl01.
  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(20) text_p01 FOR FIELD p_1.
    PARAMETERS:  p_1 RADIOBUTTON GROUP g1 DEFAULT 'X'.
  SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(20) text_p02 FOR FIELD p_2.
    PARAMETERS:  p_2 RADIOBUTTON GROUP g1.
  SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(20) text_p03 FOR FIELD p_3.
    PARAMETERS:  p_3 RADIOBUTTON GROUP g1.
  SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK bl01.

INITIALIZATION.
  text_p01 = 'MESSAGE-RAISING'.
  text_p02 = 'MESSAGE - not handled'.
  text_p03 = 'MESSAGE - handled'.

START-OF-SELECTION.


  CASE 'X'.
    WHEN p_1.
      CALL FUNCTION 'Z_MVOY_ERROR'
        EXPORTING
          iv_is_normal_exception = 'X'
        EXCEPTIONS
          normal_exception       = 1
          OTHERS                 = 2.
      IF sy-subrc <> 0.
        WRITE: 'Normal exception created in FM with MESSAGE ... RAISING'.
        WRITE:/ 'SY-SUBRC: ', sy-subrc.
        WRITE:/ 'Message: ', sy-msgv1.
      ENDIF.

    WHEN p_2.
      CALL FUNCTION 'Z_MVOY_ERROR'
        EXPORTING
          iv_is_normal_exception = ' '
        EXCEPTIONS
          normal_exception       = 1
          OTHERS                 = 2.

*     These lines will never be reached
      IF sy-subrc <> 0.
        WRITE: 'Error in FM with MESSAGE without clause RAISING'.
        WRITE:/ 'SY-SUBRC: ', sy-subrc.
        WRITE:/ 'Message: ', sy-msgv1.
      ENDIF.

    WHEN p_3.
      CALL FUNCTION 'Z_MVOY_ERROR'
        EXPORTING
          iv_is_normal_exception = ' '
        EXCEPTIONS
          normal_exception       = 1
          error_message          = 98
          OTHERS                 = 99.

      IF sy-subrc = 98.
        WRITE: 'Error in FM with MESSAGE without clause RAISING ... caught in caller program'.
        WRITE:/ 'SY-SUBRC: ', sy-subrc.
        WRITE:/ 'Message: ', sy-msgv1.
      ENDIF.

  ENDCASE.

One thought on “ABAP – catch error messages from function modules

Leave a Reply