1. RAISE
EXCEPTION TYPE class.
2. RAISE EXCEPTION ref.
Triggers a class-based exception.
RAISE EXCEPTION TYPE class.
... EXPORTING p1 = f1 ... pn = fn
class is an exception class which must directly or indirectly inherit from one of the classes CX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK.
The statement RAISE EXCEPTION TYPE class creates an object of the class class and triggers an exception of this type. If the exception occurs within a protected area, the system searches for a suitable handler in the call hierarchy and continues to process the program at this point. If no suitable handler is found, a runtime error occurs.
The handler gets access to the exception object by using the addition ... INTO ex for the CATCH clause. The reference variable ex must have a suitable type, that is, the type of the actual exception class or of a superclass.
If the exception class has a constructor, this is called after the exception object is created (see
also the note under CREATE OBJECT).
CLASS SOME_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: m1, m2.
ENDCLASS.
CLASS CX_SOME_EXCEPTION DEFINITION
INHERITING
FROM CX_STATIC_CHECK.
...
ENDCLASS.
CLASS SOME_CLASS IMPLEMENTATION.
METHOD m1.
...
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.
START-OF-SELECTION.
TRY.
CREATE OBJECT c1.
c1->m1( ).
CATCH CX_ROOT INTO ex. " /**/
... " Handler for all exceptions
ENDTRY.
An exception class CX_SOME_EXCEPTION is created which
is the successor of CX_STATIC_CHECK. An exception of this
type is triggered whenever the method m2 of the class
SOME_CLASS is called. The system creates an object of
this class within a protected area and calls the method m1
which in turn calls the method m2. If the exception is
triggered, the system searches for a suitable handler since the exception occurs within a protected
area. The handler is found at
/**/ since CX_SOME_EXCEPTION
is the indirect successor of CX_ROOT. As a result, the
current methods m1 and m2
are terminated and the program continues to process the program at /**/.
Since the addition INTO is used for the CATCH clause, the handler gets access to the exception object.
... EXPORTING p1 = f1 ... pn = fn
You can use the EXPORTING addition to pass the actual
parameters
f1 ... fn to the IMPORTING
parameters p1 ... pn of the instance
constructor of the exception class. The actual parameters must have the correct type. Global exception
classes created with the Exception Builder always have optional constructor parameters
only. If you create local exception classes whose constructor contains non-optional parameters as well,
then all of these parameters must always be filled using the EXPORTING addition.
CLASS SOME_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: m1, m2.
ENDCLASS.
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.
super->constructor( ).
me->meth
= meth.
ENDMETHOD.
ENDCLASS.
CLASS SOME_CLASS IMPLEMENTATION.
METHOD
m1.
...
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.
START-OF-SELECTION.
TRY.
CREATE OBJECT c1.
c1->m1( ).
CATCH CX_SOME_EXCEPTION INTO ex.
WRITE: 'Exception in Method', ex->meth.
ENDTRY.
The attribute meth is added to the user-defined exception
class CX_SOME_EXCEPTION which is used to display the method
in which the exception was triggered. A corresponding constructor has been created which sets the attribute
when the object is created. Using the EXPORTING addition
for RAISE EXCEPTION TYPE, you can fill the constructor with the correct parameter value.
RAISE EXCEPTION ref.
This variant of the statement RAISE EXCEPTION triggers
an exception with an existing exception object that can be reached through the object reference
ref. The variant is used to trigger exceptions already caught and to separate object creation from triggering the exception.
CLASS SOME_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: m1, m2.
ENDCLASS.
CLASS SOME_CLASS IMPLEMENTATION.
METHOD m1.
...
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.
START-OF-SELECTION.
TRY.
CREATE OBJECT c1.
c1->m1( ).
CATCH CX_SOME_EXCEPTION INTO ex.
WRITE: 'Exception in Method', ex->meth.
RAISE EXCEPTION ex.
ENDTRY.
As in the example in variant 1, the system triggers an exception of the type
CX_SOME_EXCEPTION
when the method m2 of SOME_CLASS
is called. The exception object is not created implicitly by calling RAISE
EXCEPTION TYPE but explicitly through CREATE OBJECT
and bound to the local reference variable ex. This object
is then passed to RAISE EXCEPTION which triggers the exception.
This procedure is applied again for the handler. CATCH CX_SOME_EXCEPTION
INTO ex
assigns the exception object to the reference variable ex
so that the attribute meth can then be accessed. After the method name is output, the same exception is triggered again.
Class-Based Exceptions