ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Exception Handling → Class-Based Exceptions
RAISE EXCEPTION
Other versions: 7.31 | 7.40 | 7.54
Syntax
RAISE [RESUMABLE] EXCEPTION
{ {TYPE cx_class [
message] [EXPORTING p1 = a1 p2 = a2 ...]}
| oref }.
Extras
2. ... EXPORTING p1 = a1 p2 = a2 ...
Effect
This statement interrupts execution of the current statement block and raises a
class-based exception. It can be used at any point in a
processing block. The statement interrupts the program flow and searches for a
handler as described in
System Response After
a Class-Based Exception. Depending on the definition of the handler, the context of the exception is closed before or after the handler is executed. Some
cleanup tasks may be performed here. During handling, processing
can only be resumed again after the statement RAISE EXCEPTION (and without closing the context) if the addition RESUMABLE is specified.
-
If the addition
TYPEis specified, an exception of exception class cx_class is raised and, if necessary, an exception object is created. Every exception classcx_classvisible at this point can be specified afterTYPE.
- The addition
EXPORTINGcan be used to assign actual parameters to the input parameters of the instance constructor.
-
If
orefis specified, no new exception object is created when the exception is raised. Fororef, an object reference variable must be specified that references an existing exception object. The static type oforefmust be an exception class (namely a subclass of CX_ROOT or the class itself).orefis a general expression position. In the existing exception object, the internal attributes that describe the position of the exception and that are read using the method GET_SOURCE_POSITION are applied at the position of the statementRAISE.
The statement RAISE EXCEPTION must not be used in a method or function module in whose interface
non-class-based exceptions are declared. Also,
this statement does not permit simultaneous use of the statement CATCHSYSTEM-EXCEPTIONS for the obsolete handling of
catchable runtime
errors or RAISE or
MESSAGE RAISING for raising non-class-based exceptions in function modules and methods in the current processing block.
Notes
-
If the addition
TYPEis used, exception objects are only created on demand for performance reasons, as in the following cases:
- A suitable
CATCHblock orCLEANUPblock is specified in a wrapperTRYcontrol structure with the additionINTO.
- The exception is not caught and a runtime error occurs.
-
If
orefis specified, either an exception object instantiated usingNEWor CREATE OBJECT can be used, or an exception that was previously caught during exception handling can be raised again. -
If a caught exception is raised again, note that the exception object does not remain unmodified and
that the information about the position of the exception is changed. If the original information is
to be propagated to an external handler, a new exception from the same class can be raised. The original
exception object is then passed to the parameter PREVIOUS of the constructor of this class. It may be
enough to propagate the original exception implicitly (and not raise it again using
RAISE). The associated original exception object can then be evaluated in theCLEANUPblock, if required -
The exception class after
TYPEcan be specified only statically. To raise an exception dynamically, the dynamic variant of CREATE OBJECT can be used to create an exception object and the object specified usingoref. - If a procedure is exited by raising an exception, the content of the formal parameter for which the pass by value is defined is not assigned to the respective actual parameters.
-
The addition
THROWin a conditional expression makes it possible to raise a class-based exception in an operand position.
Example
Raises an exception cx_demo in a method.
CLASS cx_demo DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS cls DEFINITION.
PUBLIC SECTION.
CLASS-METHODS meth RAISING cx_demo.
ENDCLASS.
CLASS cls IMPLEMENTATION.
METHOD meth.
...
RAISE EXCEPTION TYPE cx_demo.
...
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
cls=>meth( ).
CATCH cx_demo.
cl_demo_output=>display( 'Catching exception' ).
ENDTRY.
Example
Raises a caught exception of the class cx_demo again using the exception object.
CLASS cx_demo DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
START-OF-SELECTION.
TRY.
TRY.
RAISE EXCEPTION TYPE cx_demo.
CATCH cx_demo INTO DATA(exc).
cl_demo_output=>write( 'Inner CATCH' ).
RAISE EXCEPTION exc.
ENDTRY.
CATCH cx_demo.
cl_demo_output=>write( 'Outer CATCH' ).
ENDTRY.
cl_demo_output=>display( ).
Example
Raises an exception using a dynamically created exception object.
DATA exception TYPE seoclass-clsname VALUE 'CX_SY_ZERODIVIDE'.
cl_demo_input=>request( CHANGING field = exception ).
DATA oref TYPE REF TO cx_root.
exception = to_upper( exception ).
TRY.
CREATE OBJECT oref TYPE (exception).
CATCH cx_root INTO DATA(exc).
cl_demo_output=>display( exc->get_text( ) ).
RETURN.
ENDTRY.
TRY.
RAISE EXCEPTION oref.
CATCH cx_root INTO exc.
cl_demo_output=>display( exc->get_text( ) ).
ENDTRY.
Executable Example
Addition 1
... RESUMABLE
Effect
The addition RESUMABLE raises an exception as a
resumable exception.
When an exception of this type is handled in a CATCH
block, the statement RESUME can
be used to jump back to directly before the raising statement, as long as the context of the exception was not deleted before the exception was handled.
Notes
-
If the statement
RESUMABLEis used to raise an exception as a resumable exception, the handler has to determine whether processing is resumed afterRAISE EXCEPTION, or whether processing for the current context is canceled completely. Both alternatives can occur when an exception is raised. It is important to note thatCLEANUPblocks are only executed when the context is deleted. -
When exceptions of the types CX_STATIC_CHECK and CX_DYNAMIC_CHECK (which are raised as resumable) are
propagated, they can become non-resumable if the addition
RESUMABLEis not specified in each interface involved for the additionRAISINGto declare the exception. -
When exceptions of type CX_NO_CHECK are propagated, the resumable attribute is always retained. However,
caution should be used when raising exceptions of type CX_NO_CHECK as resumable, and it is important to ensure a procedure always displays the required behavior.
Example
Raises a resumable exception cx_demo using the addition RESUMABLE in a method.
CLASS cx_demo DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS cls DEFINITION.
PUBLIC SECTION.
CLASS-METHODS meth RAISING RESUMABLE(cx_demo).
ENDCLASS.
CLASS cls IMPLEMENTATION.
METHOD meth.
...
RAISE RESUMABLE EXCEPTION TYPE cx_demo.
cl_demo_output=>display( 'Resumed ...' ).
...
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
cls=>meth( ).
CATCH BEFORE UNWIND cx_demo.
RESUME.
ENDTRY.
Addition 2
... EXPORTING p1 = a1 p2 = a2 ...
Effect
The addition EXPORTING can be used to assign suitable actual parameters to
the input parameters of the instance constructor of the exception class. The syntax is the same as for the statement CREATE OBJECT.
As in regular method calls, a1, a2, ... are
general expression positions. In other words, functions and expressions can be passed as actual parameters, alongside data objects.
Special rules apply in this case.
Note
Only the constants of the exception class that specify a static
exception text of the
exception class should be passed to the TEXTID input parameters of the instance constructor of the exception
class. The addition MESSAGE
is used to associate any messages with an exception. This addition cannot be specified with the parameter TEXTID.
Example
A predefined exception is raised explicitly for which an exception text other than the standard exception text is selected and whose placeholder &TOKEN& is filled by passing a value to the attribute with the same name.
TRY.
...
RAISE EXCEPTION TYPE cx_sy_dynamic_osql_semantics
EXPORTING textid = cx_sy_dynamic_osql_semantics=>unknown_table_name
token = 'Test'.
...
CATCH cx_sy_dynamic_osql_semantics INTO DATA(exc).
MESSAGE exc->get_text( ) TYPE 'I'.
ENDTRY.