Skip to Content
avatar image
Former Member

Exception handling in Cleanup part of Try-Catch-Cleanup


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 :

        me->create_9060_entry( im_objid = lv_objid ).
CATCH zcx_hr_general_exception INTO lx_gen_exception.
        RAISE EXCEPTION lx_gen_exception.
      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'.

Thank you
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Dec 21, 2016 at 07:57 AM

    If you get an error why not just rollback work rather than attempting another database write ?


    Add comment
    10|10000 characters needed characters exceeded

  • Dec 21, 2016 at 08: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"?).


    Add comment
    10|10000 characters needed characters exceeded