'Catch errors of SUBMIT statement

I have a number of ABAP programs which are called dynamically using submit. To make sure it'll run, I do a syntax check first.

syntax-check for program programName message error line location word word.
if ( sy-subrc = 0 ).
  submit (programName) exporting list to memory and return.
endif.

The issue I have though is due to a logical error. One of the programs ends up trying to divide a number by zero. I don't know why or if I can fix that error, but what I'd like to do is gracefully be able to tell my application an error has happend instead of bringing the entire application to a halt.

For arguments sake, let's say the program is:

report.
data(holeInEarth) = 1 / 0.

I've tried using a try catch block but if an error occurs nothing happens.

try.
    submit (programName) exporting list to memory and return.
catch cx_root into (err).
    ...do something with err...
endtry.

I also tried using catch system-exceptions.

catch system-exceptions others = 1.
    submit (programName) exporting list to memory and return.
endcatch.

Running the report in the background isn't an option because I need the result. Is there a way to catch errors from this statement?



Solution 1:[1]

Agreed with @suncatcher about no, SUBMIT exceptions can't be handled, the external session fails (the whole SUBMIT chain, if any, fails) and the external session is restarted from zero (after showing the short dump issued from the uncaught exception).

Also agreed with @florian that this is a trick, and the solution is to correct the divide by zero bug.

But you can start the SUBMIT in a new external session opened via RFC, any short dump will return a SYSTEM_FAILURE exception :

1) Create a Z RFC-enabled function module, and make it SUBMIT the other program

SUBMIT ... WITH ... " eventual parameters

2) Call it from your program

CALL FUNCTION 'Z...'  " will do the SUBMIT
    DESTINATION 'NONE'
    EXPORTING ... " eventual parameters
    EXCEPTIONS
      SYSTEM_FAILURE = 1.
IF sy-subrc = 1.
  " there was a short dump
ENDIF.

Solution 2:[2]

Is there a way to catch errors from this statement?

No. SUBMIT produces only non-handleable exceptions, and COMPUTE_INT_ZERODIVIDE is mentioned as example even in the documentation. Read more about exception concept of ABAP.

The only solutions here are:

  1. Wrap SUBMIT into CALL TRANSACTION statement and collect errors into tab

    CALL TRANSACTION 'MEQ1' USING i_bdcdata MODE 'N'
                      MESSAGES INTO i_messtab.
    

    You can do something like this

    TYPES t_itab TYPE STANDARD TABLE OF char5
                          WITH DEFAULT KEY.
    
    DATA: messtab TYPE TABLE OF bdcmsgcoll,
          i_bdcdata TYPE TABLE OF bdcdata_tab.
    
    DATA(tab) = VALUE t_itab( ( 'MM03' ) ( 'Z0001' ) ( 'MIGO' ) ).
    
    LOOP AT tab ASSIGNING FIELD-SYMBOL(<fs>).
    CALL TRANSACTION <fs> USING i_bdcdata MODE 'N'
                                MESSAGES INTO messtab.
    ENDLOOP.
    

enter image description here

  1. Use SUBMIT with EXPORTING LIST TO MEMORY clause, which allows you to retrieve messages from spool without interrupting the program.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sandra Rossi
Solution 2