12-21-2016 7:25 AM
Hello
I use the Try-Catch-Cleanup concept for exception handling. In the Cleanup, I call a method to revert some database creations that have been made in the Try part.
This method can raise an exception when the reverting fails. I want to inform the calling code that something went wrong during the creation and cleaning up of my objects.
I get the message 'exception not handled in cleanup'.
Does anyone know a clean way of doing this ?
This is my code :
TRY.
me->create_9060_entry( im_objid = lv_objid ).
CATCH zcx_hr_general_exception INTO lx_gen_exception.
RAISE EXCEPTION lx_gen_exception.
CLEANUP.
TRY.
me->revert_9k_object_creation( im_object_id = rt_objid ).
CATCH zcx_hr_general_exception INTO data(lx_gen_cleanup_exception).
RAISE EXCEPTION TYPE zcx_hr_general_exception EXPORTING message = 'error while cleaning up'.
ENDTRY.
ENDTRY.
Thank you
Koen
12-21-2016 7:36 AM
I get the message 'exception not handled in cleanup'.
To me it is not clear if you mean syntax error or runtime error?
12-21-2016 7:57 AM
If you get an error why not just rollback work rather than attempting another database write ?
Rich
12-21-2016 8:56 AM
A CLEANUP block must be "must be executed completely and must be exited using ENDTRY so that the exception can be propagated to its handler. If an attempt is made to exit the context of a CLEANUP block prematurely, a runtime error occurs."
You cannot transport information from within a cleanup block using exceptions. But why do you want to do that at all? You must have an outer TRY block with a handler for the exception that lead to the cleanup block anyway. Use the CATCH block there to inform the calling code (by mapping the original exception to an "information exception"?).
Horst