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?
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.