Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Exception handling in Cleanup part of Try-Catch-Cleanup

Former Member

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
3 REPLIES 3

SuhaSaha
Advisor
Advisor
0 Kudos
I get the message 'exception not handled in cleanup'.

To me it is not clear if you mean syntax error or runtime error?

former_member182550
Active Contributor
0 Kudos

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

Rich

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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