Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Exception Handling →  Class-Based Exceptions →  TRY 

CLEANUP

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


CLEANUP [INTO oref]. 

Addition

... INTO oref

Effect

Introduces a statement block of a TRY control structure where cleanups can be performed.

A CLEANUP block is executed when a class-based exception in the TRY block of the same TRY control structure is raised, but is handled in a CATCH block of an external TRY control structure. A CLEANUP block is executed immediately before the context of the exception is deleted:

  • If the addition BEFORE UNWIND is specified for the handling CATCH block, the context is deleted when the CATCH block is exited and the CLEANUP block is executed accordingly after handling.
  • If the addition BEFORE UNWIND is not specified, the context is deleted before the CATCH block is executed and the CLEANUP block is executed accordingly.
  • If RESUME is used to resume processing after a resumable exception, the context is not deleted and accordingly no CLEANUP block is executed.

The CLEANUP block 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. A CLEANUP block cannot contain any statements where the system can knows statically that it cannot return to the CLEANUP block. Program calls using SUBMIT and CALL TRANSACTION should also be avoided here.


Notes

  • The context of the TRY block can be cleaned up in a CLEANUP block. For example, objects can be updated to a consistent state or external resources released to which an external handler would no longer have access.
  • Since a CLEANUP block must always be executed completely, all the exceptions raised there must also be handled there.

Example

In an inner TRY block, either the exception cx_demo0 or cx_demo1 can be raised. If cx_demo1 is raised, the CLEANUP block of the inner TRY block is executed before handling in the outer TRY block.

TYPES: 
  BEGIN OF ENUM exception, 
    cx_demo0, 
    cx_demo1, 
  END OF ENUM exception. 

CLASS cx_demo0 DEFINITION INHERITING FROM cx_static_check. 
ENDCLASS. 
CLASS cx_demo1 DEFINITION INHERITING FROM cx_static_check. 
ENDCLASS. 

START-OF-SELECTION. 
  DATA(exception) = cx_demo1. 
  cl_demo_input=>request( CHANGING field = exception ). 

  DATA exc TYPE REF TO cx_static_check. 
  exc = COND #( WHEN exception = cx_demo0 THEN NEW cx_demo0( ) 
                ELSE NEW cx_demo1( ) ). 

  TRY. 
      TRY. 
          RAISE EXCEPTION exc. 
        CATCH cx_demo0. 
          cl_demo_output=>write( 'Catching cx_demo0' ). 
        CLEANUP. 
          cl_demo_output=>write( 'Cleanup' ). 
      ENDTRY. 
    CATCH cx_demo1. 
      cl_demo_output=>write( 'Catching cx_demo1' ). 
  ENDTRY. 
  cl_demo_output=>display( ). 

Executable Examples

Addition

... INTO oref

Effect

If the addition INTO is specified, a reference to the exception object is saved to oref. The following can be specified for oref:

  • An existing object reference variable of the type CX_ROOT
  • An inline declaration DATA(var), where an object reference variable of the type CX_ROOT is declared.

oref can be used to access the exception object.


Notes

  • Within the CLEANUP block, do not raise the current exception again using RAISE EXCEPTION oref, since this would modify the attributes of the exception object.
  • Within a CLEANUP block, the attribute IS_RESUMABLE of the exception object is undefined.

Example

The CLEANUP block of the inner TRY block is executed before handling of the exception cx_demo1 in the outer TRY block. The reference variable exc points to the exception object of the class cx_demo1.

CLASS cx_demo0 DEFINITION INHERITING FROM cx_static_check. 
ENDCLASS. 
CLASS cx_demo1 DEFINITION INHERITING FROM cx_static_check. 
ENDCLASS. 

START-OF-SELECTION. 
  TRY. 
      TRY. 
          RAISE EXCEPTION TYPE cx_demo1. 
        CATCH cx_demo0. 
          ... 
        CLEANUP INTO data(exc). 
          cl_demo_output=>display( 
           cl_abap_classdescr=>get_class_name( p_object = exc ) ). 
      ENDTRY. 
    CATCH cx_demo1. 
      ... 
  ENDTRY.