ABAP Keyword Documentation →  ABAP − Reference →  User Dialogs →  Messages →  MESSAGE → 

Sending a Message Using an Interface Reference Variable

This example demonstrates how to send a message using an interface reference variable.

Source Code

REPORT demo_message_oref.

CLASS lcx_exception DEFINITION INHERITING FROM cx_dynamic_check.
  PUBLIC SECTION.
    INTERFACES if_t100_message.
    METHODS constructor IMPORTING text TYPE csequence.
    DATA text TYPE c LENGTH 40.
ENDCLASS.

CLASS lcx_exception IMPLEMENTATION.
  METHOD constructor.
    super->constructor( ).
    me->text = text.
    if_t100_message~t100key-msgid = 'SABAPDEMOS'.
    if_t100_message~t100key-msgno = '888'.
    if_t100_message~t100key-attr1 = 'TEXT'.
  ENDMETHOD.
ENDCLASS.

CLASS msg_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-METHODS meth
      RETURNING value(msg_ref) TYPE REF TO if_t100_message.
ENDCLASS.

CLASS msg_demo IMPLEMENTATION.
  METHOD main.
    MESSAGE meth( ) TYPE 'I' DISPLAY LIKE 'E'.
  ENDMETHOD.
  METHOD meth.
    DATA exc_ref TYPE REF TO lcx_exception.
    TRY.
        RAISE EXCEPTION TYPE lcx_exception
          EXPORTING
            text = 'Exception!'.
      CATCH lcx_exception INTO exc_ref.
        msg_ref = exc_ref.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  msg_demo=>main( ).

Description

The operand msg of the statement MESSAGE is passed a functional method meth whose return code is a reference variable of the type IF_T100_MESSAGE. The method fills this reference variable with a reference to an exception object of a local exception class.

The local exception class lcx_exception implements the interface IF_T100_MESSAGE. It fills the attribute T100KEY of this interface with values that take a certain message from the table T100 and that replace the placeholder "&" in the message with the content of the attribute text. The attribute text is filled when the exception is raised.