Did it ever happen that you wanted to use a standard Function Module in your application instead of rewriting the whole logic? And the function was "tailored" for classic dynpros but you were calling it in a background application ... and this little devil had the following syntax:

IF sy-subrc <> 0.
    MESSAGE 'Critical Error' TYPE 'E'.
ENDIF.

Normally, this would interrupt the processing of the application instead of letting you handle the error as you see fit. There is a way, though it's not obvious how you could catch it.

Let's say we have the following function:

FUNCTION ZTEST.
    MESSAGE 'Critical Error' TYPE 'E'.
ENDFUNCTION.

In order to catch this error, you need to call the FM with the exception ERROR_MESSAGE

CALL FUNCTION 'ZTEST'
    EXCEPTIONS
        error_message = 1.
IF sy-subrc = 1.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
        INTO DATA(lv_error).
ENDIF.

The number of the exception is not important, it can be anything you want.


If code would be always written in a correct way, no one should stumble upon this issue. But hey, this is the real world! At least we have a solution. However, I have to say, it is a rare issue. This only happened to me once in my whole programming career. And that was enough to question why I chose to become one.

More information can be found in the ABAP Documentation.