Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Exception Handling →  Class-Based Exceptions →  RAISE EXCEPTION 

RAISE EXCEPTION - message

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { MESSAGE tn(id) 
            | { ID mid TYPE mtype NUMBER num }
              [WITH dobj1 ... dobj4] }
  | { USING MESSAGE } ...

Variants

1. ... MESSAGE msg ...
2. ... USING MESSAGE ...

Effect

The addition MESSAGE of the statement RAISE EXCEPTION and of the addition THROW in a conditional expression passes a message, if specified, to the exception object.

Variant 1

... MESSAGE msg ...

Effect

The addition tn(id) or ID mid TYPE mtype NUMBER num is used to specify the message type, the message class, and the message number of a message msg for the table T100, statically or dynamically. This is done in the same way as in the statement MESSAGE. Also like in MESSAGE, the optional addition WITH can be used to fill the placeholders of a message. The exception class of the raised exception must include one of the system interfaces for messages:

The addition MESSAGE fills the attributes of these interfaces with values. This assignment is made after the instance constructor is executed. This overwrites any values assigned to these attributes when the exception object was constructed.

The attributes of the system interfaces are used to associate the exception object with the message specified after MESSAGE. Using an object reference, the object can be used directly in the variant MESSAGE oref of the MESSAGE statement and the message texts can be read using the interface methods.

If the addition MESSAGE is used, the input parameter TEXTID of the constructor of the exception class must not be filled. This parameter is only designed for specifying a predefined exception text.


Notes

Using IF_T100_DYN_MSG

The full functions of the addition MESSAGE are available only if the system interface IF_T100_DYN_MSG is included in the exception class in question. This interface covers the system interface IF_T100_MESSAGE requested by the syntax check. When an exception class is used with the system interface IF_T100_DYN_MSG, the addition MESSAGE fills its attributes as follows after the instance constructor is executed:

  • The message type specified after MESSAGE is assigned to the attribute MSGTY of the interface IF_T100_DYN_MSG.
  • The components of the structured attribute T100KEY of the component interface IF_T100_MESSAGE are filled as follows:
  • MSGID is assigned the message class specified after MESSAGE.
  • MSGNO is assigned the message number specified after MESSAGE.
  • ATTR1 to ATTR4 are assigned the names IF_T100_DYN_MSG~MSGV1 to IF_T100_DYN_MSG~MSGV4 of the interface attributes for the placeholders of the message. If the addition WITH is not specified, ATTR1 to ATTR4 are kept initial.
  • The attributes MSGV1 to MSGV4 of the interface IF_T100_DYN_MSG are assigned the texts specified using WITH.


Notes

  • If IF_T100_DYN_MSG is used, the addition MESSAGE assigns the correct values to all required attributes of the exception class to associate them with the message. More specifically, the attributes for the placeholders of the message are mapped to the structure T100KEY of the interface IF_T100_MESSAGE automatically.
  • In exception classes with the interface IF_T100_DYN_MSG, MESSAGE used together with the EXPORTING addition can produce unexpected results in the placeholder texts if the addition WITH is not specified. Implicit assignments of the input parameters specified after EXPORTING to the components ATTR1 to ATTR4 are intended for exception classes with the interface IF_T100_MESSAGE where WITH cannot be specified (see the executable example). Implicit assignments of the names of the input parameters or of initial values can be prevented by specifying WITH with a dummy value space.

Example

Raises the exception CX_DEMO_DYN_T100 that includes the interface IF_T100_DYN_MSG. The addition MESSAGE is used to pass the attributes of a message that determines the exception text.

TRY. 
    RAISE EXCEPTION TYPE cx_demo_dyn_t100 
      MESSAGE ID 'SABAPDEMOS' 
      TYPE 'I' 
      NUMBER '888' 
      WITH 'Message'. 
  CATCH cx_demo_dyn_t100 INTO DATA(oref). 
    cl_demo_output=>display( oref->get_text( ) && 
                             `, ` && oref->msgty ). 
ENDTRY.

Executable Examples

Using IF_T100_MESSAGE

If only the system interface IF_T100_MESSAGE is included, the addition WITH must not be used and only a restricted set of functions is available. When an exception class is used that includes only the system interface IF_T100_MESSAGE, the addition MESSAGE fills its attributes as follows after the instance constructor is executed:

  • If specified, message types are ignored.
  • The components of the structured attribute T100KEY of the component interface IF_T100_MESSAGE are filled as follows:
  • MSGID is assigned the message class specified after MESSAGE.
  • MSGNO is assigned the message number specified after MESSAGE.
  • ATTR1 to ATTR4 are assigned the names of the first four input parameters of the instance constructors specified after EXPORTING that can be converted to a character-like target field, in the order given. Components that cannot be assigned input parameters are initialized.


Notes

  • It is recommended that the system interface IF_T100_DYN_MSG is used for exceptions raised using the addition MESSAGE.
  • The use of the addition MESSAGE for an exception class with IF_T100_MESSAGE is designed for cases where existing exception classes that cannot be switched to IF_T100_DYN_MSG need to be used.
  • The addition MESSAGE is suitable for existing exception classes with IF_T100_MESSAGE that already have a generic exception text without semantic meaning. Until now, structures of the type SCX_T100KEY, filled in the program, had to be passed to exceptions of this type.
  • The use of the addition MESSAGE is not recommended for an exception class with IF_T100_MESSAGE whose exception texts all have a fixed semantic meaning.

Executable Example

System Interface IF_T100_MESSAGE for Exception with Message

Variant 2

... USING MESSAGE ...

Effect

This variant is a short form of the preceding variant of the MESSAGE addition. It can be used if the exception class of the raised exception includes the system interface IF_T100_DYN_MSG, and has the following effect:

... MESSAGE ID     sy-msgid
            TYPE   sy-msgty
            NUMBER sy-msgno
            WITH   sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 ...

The addition USING MESSAGE implicitly passes the specification of the message that is saved during the execution of the statement in the system fields sy-msgid, sy-msgty sy-msgno, and sy-msgv1 to sy-msgv4 to the exception class.


Note

The short form is particularly well suited to converting classic exceptions that are raised in function modules or methods with the statement MESSAGE RAISING, or messages that are caught with error_message, into class-based exceptions.


Example

Raises the exception CX_DEMO_DYN_T100 that includes the interface IF_T100_DYN_MSG. The addition USING MESSAGE implicitly passes the attributes of a status message that was output with the statement MESSAGE.

MESSAGE ID 'SABAPDEMOS' TYPE 'S' 
        NUMBER '888' 
        WITH 'Message'. 
TRY. 
    RAISE EXCEPTION TYPE cx_demo_dyn_t100 USING MESSAGE. 
  CATCH cx_demo_dyn_t100 INTO DATA(oref). 
    cl_demo_output=>display( oref->get_text( ) && 
                            `, ` && oref->msgty ). 
ENDTRY.

Executable Examples

Continue

Converting a Classic Example to a Class-Based Exception

Converting the Exception error_message to a Class-Based Exception