Skip to Content
0

Exception handling in Cleanup part of Try-Catch-Cleanup

Dec 21, 2016 at 07:25 AM

176

avatar image

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
10 |10000 characters needed characters left characters exceeded
I get the message 'exception not handled in cleanup'.

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

0
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Richard Harper Dec 21, 2016 at 07:57 AM
0

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

Rich

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Dec 21, 2016 at 08:56 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded