ABAP: Eliminating Errors by using exception ERROR_MESSAGE

Home / SAP / ABAP Coding / ABAP: Eliminating Errors by using exception ERROR_MESSAGE

I have to thank Jeremy Meier for this post.  I have to confess, I’m pretty excited to have my first guest post, so I just wanted to preface his post with a hearty thank you 🙂  The exception ERROR_MESSAGE is a technique I was unfamiliar with until Jer sent this to me.

I discovered this for one of our developers today – he was getting errors in using a standard SAP function module to get material requirements data from an MD04 function module for a report he was writing.  The code was calling a standard message when it hit a rare error check in the code”

if xyz <> abc.
MESSAGE ID ‘123’ TYPE ‘E’ NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

As this is used in MD04, it’s meant to throw the message to the screen – but when he tried to embed this function module in his Z-Report and ran it on a specific material number it caused an error to occur on the expected automated file download to excel to fail.

After looking for the right TRY-CATCH statement – which wouldn’t actully work because there was no “exception” or dump – the solution after digging a bit was to include the exception ERROR_MESSAGE to his function module call, even though it wasn’t a “defined exception” on the function header:

CALL FUNCTION ‘XYZ’
EXPORTING
VALUE1 = VARIABLE1
IMPORTING
VALUE2 = VARIABLE2
EXCEPTIONS
EXCEPTION_A = 1
EXCEPTION_B = 2
ERROR_MESSAGE = 3.

This action trapped the message and saved it to the SYST table where a standard FM can then be used to extract it if necessary, if the function module returns with sy-subrc=3:

if sy-subrc = 3.
CALL FUNCTION ‘BALW_BAPIRETURN_GET2’
EXPORTING
type  = sy-msgty
cl    = sy-msgid
number = sy-msgno
par1  = sy-msgv1
par2  = sy-msgv2
par3  = sy-msgv3
par4  = sy-msgv4
IMPORTING
return = return.
endif.

Realistically, anytime your using SAP function modules that are part of transactions, developers should try to us this to suppress the error messages and return them on their terms.

As always, thanks for reading and don't forget to check out our SAP Service Management Products at my other company JaveLLin Solutions,
Mike

One thought on “ABAP: Eliminating Errors by using exception ERROR_MESSAGE

Leave a Reply

Your email address will not be published. Required fields are marked *