Skip to content

ABAP Keyword Documentation →  ABAP Programming Guidelines →  Architecture →  Error Handling 

Exception Texts

Other versions: 7.31 | 7.40 | 7.54

Background

Each global exception class has a predefined exception text that has the same name as the exception class. The predefined text can be edited in Class Builder on the Texts tab page and additional exception texts can be defined. The exception texts of an exception class can be created either by referencing messages or as texts in the OTR (Online Text Repository).

For each exception text, Class Builder creates a static constant in the exception class with the same name as the exception text. When the exception is raised, this can be passed to the parameter TEXTID of the instance constructor to determine the exception text. If the parameter is not passed, the predefined exception text with the same name as the exception class is used.

From a technical perspective, the parameter TEXTID of the instance constructor can also be used to pass messages or texts from OTR as exception texts.

Rule

Create suitable exception texts in the exception class and only use these texts

When you create an exception class, make an informed decision about the text category (messages for user dialogs or an OTR text for internal texts) in which you create the exception texts. Only the associated texts can be used when an exception is raised.

Details

The following guideline tells you how to choose the text category:

  • Message texts should only be used if the text is to be sent to the program user. This might be the case with application programs but should generally be avoided in system programs. One other disadvantage of using message short texts is that they are limited to 73 characters.
  • Texts from OTR should mainly be used in system programs where text is not to be sent to the program user.

From a technical perspective, it is possible to pass a data object to the input parameter TEXTID of the instance constructor when an exception is raised. This data object specifies either a message or an OTR text, depending on the text category. We strongly recommend that you avoid this approach, however. If the parameter TEXTID is used, an exception can only be raised with the texts specific to it. Only the associated constants of the exception class can be passed to the input parameter TEXTID of the instance constructor.

Exception

In cases where a class-based exception is used to wrap a classic exception and MESSAGE ... RAISING is used to associate this classic exception with a message text, the class-based exception can use the same message text regardless of whether the program in question is a system program or application program.


Note

Classic exceptions are not associated with exception texts. If classic exceptions are required for reasons of downward compatibility, the statement MESSAGE ... RAISING gives the option of emulating exception texts here. Using MESSAGE ... RAISING in cases in which non-class-based exceptions must still be used is preferable to using the statement RAISE, because it offers the option of providing additional text information with an exception.

Bad example

The following source code passes a UUID for an OTR text to the input parameter TEXTID of the instance constructor when an exception is raised. According to the rule above, however, only exception texts from the exception class can be passed, with each exception class containing the corresponding constants.

...
DATA otr_id TYPE sotr_conc.
otr_id = '9753EBD0102AD0418D902B8D972083C4'.
RAISE EXCEPTION TYPE zcx_exception_text
   EXPORTING
      textid = otr_id.
...

Good example

The following source code passes the constant for the associated OTR text to the input parameter TEXTID of the instance constructor when an exception is raised, as specified by the rule above.

...
RAISE EXCEPTION TYPE zcx_exception_text
   EXPORTING
      textid = zcx_exception_text=>zcx_exception_text.
...