Skip to content

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

Creating Exception Classes

Exception classes can be defined globally in the Class Builder or locally in a program. The names of global exception classes are prefixed with CX_, YCX_, ZCX_, and so on in the case of exception classes that are created in the customer system. There is a set of predefined global exception classes such as those with the prefix CX_SY_ whose exceptions must be triggered in the runtime environment in an exception situation.

All exception classes inherit the following instance methods from CX_ROOT:

  • GET_TEXT and GET_LONGTEXT return the exception text (short and long text) as return values of the type string. Methods are defined in the IF_MESSAGE interface implemented in CX_ROOT and can be addressed using the same alias name as that of the class.
  • GET_SOURCE_POSITION returns the program name, the name of the include program if applicable, and the line number of the statement that caused the exception.

All exception classes inherit the following instance attributes from CX_ROOT:

  • TEXTID of type SOTR_CONC for a key for the OTR (Online Text Repository) or of type SCX_T100KEY for a key for the T100 database table, which defines the exception text. This is normally set by the constructor and evaluated with GET_TEXT.
  • PREVIOUS of reference type CX_ROOT, which can contain a reference to a previous exception. This is normally set by the constructor.
  • IS_RESUMABLE of type ABAP_BOOL, which uses a CATCH or CLEANUP block to show whether the exception is resumable (whether a CATCH block can be exited with RESUME).

For global exception classes, the Class Builder generates a non-changeable instance constructor that has optional input parameters for all non-private attributes and that sets these attributes to the value of the input parameters. In particular, a TEXTID can pass the ID of the required exception class. In the case of local exception classes, there are no special rules for the instance constructor.

Other versions: 7.31 | 7.40 | 7.54


Notes

  • Instances of exception classes are generally generated when exceptions are triggered but can also be instantiated with CREATE OBJECT.

  • The instance constructor of an exception class should not generate any exceptions. However, if an exception is triggered in the instance constructor during the instantiation of an exception class and that exception cannot be handled there, this exception (or in the case of unsuccessful propagation, the exception CX_SY_NO_HANDLER) replaces the previously triggered exception.

  • Additional methods and attributes can be defined in exception classes to transport additional information on an error situation to the handler, for example. The user-defined attributes should be write-protected (READ-ONLY).