Show TOC

 Creating an Exception ClassLocate this document in the navigation structure

Use

You use this procedure to create global exception classes in the Class Builder. For more information about exception classes, see the ABAP Keyword Documentation under Exception Classes.

Procedure
Tip

You can create a new exception class when you need it for the first time, for example when assigning exceptions in the method definition. See also Creating Exceptions for Methods.

  1. In the Repository Browser (transaction SE80), navigate to the package in which you want to create an exception class.
  2. In the context menu of the package, choose Create → Class Library → Class.

    The Create Class dialog box appears.

  3. In the Class Type group box, choose Exception Class.
  4. In the Class field, enter a name of the new class according to the naming conventions. The prefix CX_ should be added to the name of all exception classes.
  5. In the Description field, enter a short description of the class.
  6. Enter the rest of the basic data for the class:
    Option Description

    Superclass

    Enter the name of the direct superclass.

    Note

    Exception classes are derived from one of the predefined basic classes CX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK. Depending on the position of the exception class within the class hierarchy, enter the appropriate class within this hierarchy or one of the predefined basic classes as a superclass. In the latter case, expand a new hierarchy tree.

    Instantiation

    • The Public option is selected by default. This means that each user can create instances of the particular class (with CREATE OBJECT).
    • The Protected option defines that only inherited classes or the relevant class itself can create instances of this class.
    • If you choose the Private option, only the relevant class itself can create its instances (only using its own method).
    • You can define an abstract class with the Abstract option. You cannot create an instance for this class. An abstract class can be used as a template for your subclasses. You can only access this class with your static attributes or with your subclasses.

    Class Type

    If you assign the prefix CX_ to the name of the exception class, the class type Exception class is automatically selected. At this point, you can decide whether the texts for your newly created exception class should be recreated and stored in the OTR, or whether you want use existing message classes for this exception class.

    These options differ in terms of the flexibility of the parameterization and the cases where they are used:

    • Exception texts in the Online Text Repository (OTR)

      This type of text can be defined with any number of placeholders. Each placeholder is represented by the name of an attribute from the exception class, which is enclosed by & characters. If the exception occurs, the placeholders are replaced by the content of the attributes. The texts are stored in the OTR, can contain a maximum of 255 characters, and are used primarily in system programs where the text must not be sent to the program user.

    • Messages as exception texts

      This type of exception text can be used if the exception class implements the interface IF_T100_MESSAGE. In this case, the short texts for messages from Database Table T100 are used as exception texts. The text is identified by the message class and the message number. Attributes from the exception class can be assigned to placeholders &1 to &4 or & in the message. If the exception occurs, the placeholders are replaced by the content of the attributes. These texts can contain a maximum of 72 characters, and can be sent to the program user during exception handling with the MESSAGE oref statement.

    Final

    Specifies the development status of the object. Only objects and components with status final can have executable code. This class completes the inheritance hierarchy since a final class may not create any further subclasses.

  7. Choose Save.
  8. Enter the name of the package to which your class should belong.
  9. Choose Save.
Result

You have created a new exception class. A class pool is generated for the new class if the Only modeled option was not activated. In contrast to normal classes, you cannot create a method for an exception class. The Class Builder simply creates the constructor but you can create attributes and maintain exception texts.

See also:

Creating Attributes for Exception Classes

Creating Texts for Exceptions