Class based Exceptions IV – CLEANUP

By | January 1, 2012 | ABAP Objects, Exceptions, OO Concepts | 12,013 | 2

In the series of Class based Exceptions, lets check out the addition CLEANUP within the TRY..ENDTRY block.

Checkout all post related to Exception Raising & handling:

TRY .. ENDTRY block also should have a CLEANUP block. This code block should contain cleaning up activity before leaving the TRY .. ENDTRY block. This block would gets triggered whenever there is no CATCH statement declared for the triggered exception in the current code block but it the exception is being caught somewhere else. So, if there isn’t any CATCH statment declared in the call hierarchy and the exception occurred, CLEANUP won’t get executed and it will result in the short-dump.

CLEANUP block would be used to remove the references before leaving the method call. Whenever exception occurs, system would stop processing from that point and go to the corresponding TRY block. Because of this behavior, object would be in intermediate state. CLEANUP block provides us an opportunity to restore the object state before leaving the current processing block.

Refer to the Class based Exceptions III – Runtime flow for flow diagram – look for right bottom corner for CLEANUP block processing.

Code Lines

Lets try to understand this with help of example:


report znp_demo_excep_cleanup.
CLASS lcx_mandatory_missing DEFINITION
  INHERITING FROM cx_static_check.
ENDCLASS.                    "lcx_mandatory_missing  DEFINITIO
CLASS lcl_test_exceptions DEFINITION.
    METHODS: calculate IMPORTING iv_num1 TYPE i OPTIONAL
                               iv_num2 TYPE i OPTIONAL
                     RETURNING value(rv_calc) TYPE i.
    METHODS: do_div.
    DATA: v_num1 TYPE i,
          v_num2 TYPE i,
          v_result TYPE i.
ENDCLASS.                    "lcl_test_exceptions DEFINITION
CLASS lcl_test_exceptions IMPLEMENTATION.
  METHOD calculate.
    v_num1 = iv_num1.
    v_num2 = iv_num2.
    me->do_div( ).
    rv_calc = v_result.
  ENDMETHOD.                    "calculate
  METHOD do_div.
        v_result = v_num1 DIV v_num2.
* This CLEANUP block would trigger as we catch all the
* exceptions using the CX_ROOT in the TRY .. ENDTRY 
        CLEAR v_result.
  ENDMETHOD.                    "do_sum
ENDCLASS.                    "lcl_test_exceptions IMPLEMENTATION
  DATA: lo_obj TYPE REF TO lcl_test_exceptions.
  TRY .
      lo_obj->calculate( iv_num1 = 10 ).
    CATCH cx_root.

In this example, we have an “outer” TRY..ENDTRY block in the START-OF-SELECTION. We have an “inner” TRY..ENDTRY block within the method CALCULATE( ) which is begin called from the outer block. Since we are not passing IV_NUM2, the devide statement will raise an exception type CX_SY_ZERODIVIDE. Since we are catching all the exception using CATCH CX_ROOT in the outer TRY block, system will execute the logic of CLEANUP block within inner TRY block.

Replace this CX_ROOT with CX_SY_BUFFER_OVERFLOW which is not part of the hierarchy of CX_SY_ZERODIVIDE thus CLEANUP wont get execute and it will result in run-time error.

Further Reading

Checkout all post related to Exception Raising & handling:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments


  • Rajesh M

    Hello Naimesh,

    I see some great work from you on ABAP OO concepts,

    I am good in Core ABAP, but when it comes to OO not very good in it, can you please help me in building myself in ABAP OO. any documents where i can learn and implement them in day to day activities.

    Thanks in Advance for any help !!!\


  • Peter A.

    Hi Naimesh,

    Is there any way how to handle exception which is not propagated from called method?
    -> Exception: CX_SY_RANGE_OUT_OF_BOUNDS
    It is caused in a method LOAD of class CL_ABAP_ZIP. It occures when the .zip file is corrupted.

    I don’t know how to elegantly solve it.

    Thank you in advance.

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.