No matter how many times mentioned before (thanks to Rahul Gopinath, Amit Behera, Srinivas Dummu and Mike), I still see sy-subrc check after a function module call without exceptions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
CALL FUNCTION 'RV_INVOICE_CREATE' EXPORTING vbsk_i = vbsk_i TABLES xkomfk = xkomfk xkomv = xkomv xthead = xthead xvbfs = xvbfs xvbpa = xvbpa xvbrk = xvbrk xvbrp = xvbrp xvbss = xvbss. IF sy-subrc <> 0. * Error handling ENDIF. |
This is so wrong. According to ABAP keyword documentation:
If no exception is raised, a call sets sy-subrc to 0.
So in the above example, sy-subrc will always be equal to zero and condition will never be satisfied. This applies to method calls, too. Let’s experiment to make sure. Here is a test program…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
CLASS lcl_test DEFINITION. PUBLIC SECTION. METHODS find_nothing. ENDCLASS. CLASS lcl_test IMPLEMENTATION. METHOD find_nothing. WRITE: /'At method start:', sy-subrc. SELECT SINGLE COUNT( * ) FROM vbrk WHERE vbeln = space. WRITE: /'At method end:', sy-subrc. ENDMETHOD. ENDCLASS. DATA o_test TYPE REF TO lcl_test. START-OF-SELECTION. WRITE 'Value of sy-subrc'. WRITE: /'At start:', sy-subrc. SELECT SINGLE COUNT( * ) FROM vbrk WHERE vbeln = space. WRITE: /'After select statement:', sy-subrc. CREATE OBJECT o_test. o_test->find_nothing( ). WRITE: /'After method call:', sy-subrc. |
This test confirms ABAP keyword documentation but leaves us with a question: How do we handle errors of a function module without any exception? Let’s dive into the code of RV_INVOICE_CREATE , function module in our bad example.
376 377 378 379 380 381 382 383 |
IF NOT sy-batch IS INITIAL. "background job VF06 MESSAGE ID xvbfs-msgid TYPE xvbfs-msgty NUMBER xvbfs-msgno WITH xvbfs-msgv1 xvbfs-msgv2 xvbfs-msgv3 xvbfs-msgv4. ELSE. MESSAGE s032. ENDIF. |
It seems that the function module throws dialog messages. So if an error message is raised, the program run will stop with a message. That is okay until we need batch processing. If we need to create invoices in batch and we would like to continue processing despite an error in one of calls, our hands are not tied. SAP provides us with a hidden feature: predefined exception ERROR_MESSAGE . We can fix our first example with a simple move:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CALL FUNCTION 'RV_INVOICE_CREATE' EXPORTING vbsk_i = vbsk_i TABLES xkomfk = xkomfk xkomv = xkomv xthead = xthead xvbfs = xvbfs xvbpa = xvbpa xvbrk = xvbrk xvbrp = xvbrp xvbss = xvbss EXCEPTIONS error_message = 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. |
This exception will catch all E and A type messages and mesage details can be accessed in SY structure. See ABAP keyword documentation for more details.