RAISE EXCEPTION

Variants:

1. RAISE EXCEPTION TYPE exc_class.

2. RAISE EXCEPTION exc_ref.

Effect

Raises a class-based exception.

Variant 1

RAISE EXCEPTION TYPE exc_class.

Addition:

... EXPORTING  p1 = f1       ... pn = fn

exc_class is an exception class, which must be derived either directly or indirectly from one of the classes CX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK. The RAISE EXCEPTION TYPE exc_class statement generates an object of the class exc_class and raises an exception of this type. If an exception occurs within a protected area, the system searches along the call hierarchy for a suitable handler; the program then continues at that point. If no suitable handler is found, a runtime error occurs. The handler accesses the exception object by using the ... INTO ex addition to the CATCH clause, where the reference variable ex must have a suitable type (that is, it must have the type of the exception class, or of a superclass). If the exception class has a constructor, it is called after the exception object is generated (see also CREATE OBJECT).





Example

CLASS CX_SOME_EXCEPTION DEFINITION
  INHERITING FROM CX_STATIC_CHECK.
...
ENDCLASS.

CLASS SOME_CLASS IMPLEMENTATION.
  METHOD m1.
    ...
    CALL METHOD m2.
    ...
  ENDMETHOD.
  METHOD m2.
    RAISE EXCEPTION TYPE CX_SOME_EXCEPTION.
  ENDMETHOD.
ENDCLASS.

DATA: ex TYPE REF TO CX_ROOT,
      c1 TYPE REF TO SOME_CLASS.

TRY.
  CREATE OBJECT c1.
  CALL METHOD c1->m1.
CATCH CX_ROOT INTO ex. " /**/
  ... " Handlers for all exceptions
ENDTRY.

The example creates an exception class, CX_SOME_EXCEPTION, a subclass of CX_STATIC_CHECK. An exception of this type is raised when the method m2 of the class SOME_CLASS is called. An object of this class is generated within the protected area, and the method m1 is called. m1 then calls m2. When the exception is raised, the system searches for a suitable handler (since the exception occurs in the protected area). This is found at /**/, since CX_SOME_EXCEPTION is an indirect subclass of CX_ROOT. The current methods m1 and m2 are aborted and the program control jumps to /**/. Since the INTO addition to the CATCH clause has been used, the handler can access the exception object.

Addition 1

... EXPORTING p1 = f1 ... pn = fn

Effect

The EXPORTING addition passes the actual parameters f1 ... fn to the IMPORTING parameters p1 ... pn of the instance constructor of the exception class. The actual parameters must be appropriately typed. Global exception classes, created in the Exception Builder , only ever have optional constructor parameters. If you create local exception classes, and give their constructor non-optional parameters, you must always fill these parameters with the EXPORTING addition.

Example

CLASS CX_SOME_EXCEPTION DEFINITION
   INHERITING FROM CX_STATIC_CHECK.
PUBLIC SECTION.
   DATA: meth TYPE STRING.
   METHODS: constructor IMPORTING VALUE(meth) TYPE STRING OPTIONAL.
ENDCLASS.

CLASS CX_SOME_EXCEPTION IMPLEMENTATION.
  METHOD constructor.
    CALL METHOD super->constructor.
    me->meth = meth.
  ENDMETHOD.
ENDCLASS.

CLASS SOME_CLASS IMPLEMENTATION.
  METHOD m1.
    ...
    CALL METHOD m2.
    ...
  ENDMETHOD.
  METHOD m2.
    RAISE EXCEPTION TYPE CX_SOME_EXCEPTION EXPORTING meth = 'm2'.
  ENDMETHOD.
ENDCLASS.

DATA: ex TYPE REF TO CX_SOME_EXCEPTION,
      c1 TYPE REF TO SOME_CLASS.

TRY.
  CREATE OBJECT c1.
  CALL METHOD c1->m1.
CATCH CX_SOME_EXCEPTION INTO ex.
  WRITE: 'Exception in Method', ex->meth.
ENDTRY.

The user-defined exception class, CX_SOME_EXCEPTION, is extended to include the meth attribute, which displays the method in which the exception was raised. An appropriate constructor is created, which sets the attribute when the object is generated. The constructor receives the appropriate parameter value from the EXPORTING addition to the RAISE EXCEPTION TYPE statement.


Variant 2

RAISE EXCEPTION exc_ref.


This variant of the RAISE EXCEPTION statement raises an exception with an existing exception object that can be reached using the object reference exc_ref. The purpose of this variant is to allow exceptions that have already been caught to be raised again, and also to allow objects to be generated separately from the raising of the exception.

Example

CLASS SOME_CLASS IMPLEMENTATION.
  METHOD m1.
    ...
    CALL METHOD m2.
    ...
  ENDMETHOD.
  METHOD m2.
    DATA: ex TYPE REF TO CX_SOME_EXCEPTION.
    CREATE OBJECT ex EXPORTING meth = 'm2'.
    ...
    RAISE EXCEPTION ex.
  ENDMETHOD.
ENDCLASS.

DATA: ex TYPE REF TO CX_SOME_EXCEPTION,
      c1 TYPE REF TO SOME_CLASS.

TRY.
  CREATE OBJECT c1.
  CALL METHOD c1->m1.
CATCH CX_SOME_EXCEPTION INTO ex.
  WRITE: 'Exception in Method', ex->meth.
  RAISE EXCEPTION ex.
ENDTRY.

This is similar to the example in variant 1: it raises an exception of the type CX_SOME_EXCEPTION when the method m2of the class SOME_CLASS is called. In this case, the exception object is raised explicitly using CREATE OBJECT (rather than implicitly using a RAISE EXCEPTION TYPE call) and bound to the local reference variable ex. This object is then passed to the RAISE EXCEPTION statement that raised the exception. This mechanism is used again in the handler. The exception object is assigned to the reference variable ex using CATCH CX_SOME_EXCEPTION INTO ex, so that the meth attribute can be accessed. After the method name has been displayed, the same exception is raised again.

Related

TRY, CATCH, CLEANUP