9.4    Messages in Exception Classes

Exception classes allow us to exit an exception situation gracefully. However, sometimes an exception is caused by an error that may need human intervention to fix. In such situations, we can capture a message when the exception is raised so that the error can be identified. For this, we can pass a text message with the exception. Texts can also have dynamic field values to supplement the message.

Texts can be maintained directly in the exception class, which uses the Online Text Repository (OTR), or we can also use messages from a message class (Transaction SE91). The following subsections will look at each of these cases.

9.4.1    Using the Online Text Repository

The following steps walk you through maintaining messages in exception classes:

  1. In the Attributes tab of the exception class, define an attribute with a value you want to pass in the message (see Figure 9.19). In this example, we’re maintaining MAT_NO as the attribute name of type MATNR because we plan to pass the material number in the exception message. Once the attribute is defined, click Activate.
    Maintaining Attribute in Exception Class

    Figure 9.19    Maintaining Attribute in Exception Class

  2. Under the Texts tab, maintain an Exception ID and a Text. You can pass the attribute value in the message by keeping the attribute name between ampersands (&), as shown in Figure 9.20. Here, we’ve maintained the Exception ID as INVALID_MATERIAL and the text as The material &mat_no& is invalid by enclosing the attribute within ampersands. You can also maintain long text for the message by clicking the Long Text button. When finished, click Activate.
    Maintaining Exception ID and Text

    Figure 9.20    Maintaining Exception ID and Text

  3. At this point, under the Attributes tab, you’ll see that the Exception ID defined is added as a constant of type SOTR_CONC, as shown in Figure 9.21. The maintained text is saved in the OTR and supports internationalization (translations).
    Exception ID Added as Attribute

    Figure 9.21    Exception ID Added as Attribute

  4. The class constructor automatically adds the attributes that you’ve added as importing parameters to its signature, which can be passed while raising the exception using the EXPORTING keyword. The constructor ensures that all the attributes are initialized properly. The textid attribute stores the Exception ID to access the corresponding text. The previous attribute stores the previous exceptions in the context. Figure 9.22 shows the signature and initialization code of the constructor method after adding the mat_no attribute.
    Signature of ConstructorConstructor

    Figure 9.22    Signature of Constructor

  5. To raise the exception and pass the message with the material number, you can enhance the RAISE EXCEPTION statement used earlier, as shown in Figure 9.23. Here, you call the constructor and pass TEXTID by accessing the INVALID_MATERIAL constant defined under the attributes of the class. This constant refers to the corresponding Exception ID. You also pass the material number to the mat_no attribute so that the &mat_no& field in the text will be replaced with the value.
    Passing Message in ExceptionExceptionspassing messages

    Figure 9.23    Passing Message in Exception

When this exception is caught in the program using the code listed in Listing 9.8, it will result in the output shown in Figure 9.24.

DATA v_matnr TYPE matnr VALUE '100'.
DATA v_maktx TYPE maktx.
DATA lr_err TYPE REF TO ZCX_NO_MATERIAL.
TRY.
CALL FUNCTION 'ZDEMO_GET_MATERIAL_DESCRIPTION'
EXPORTING
im_matnr = v_matnr
im_spras = sy-langu
IMPORTING
ex_maktx = v_maktx.

CATCH ZCX_NO_MATERIAL INTO lr_err.
WRITE lr_err->get_text( ).
ENDTRY.

Listing 9.8    Catching Exception to Print Text

Output of Code in

Figure 9.24    Output of Code in Listing 9.8

9.4.2    Using Messages from a Message Class

To use messages from a message class, proceed with the following steps:

  1. Select the With Message Class checkbox, as shown in Figure 9.25.
    Class Properties Window

    Figure 9.25    Class Properties Window

  2. Once this checkbox is selected, the system automatically adds an IF_T100_MESSAGE interface to the message class, which is used to access the messages stored in message class (see Figure 9.26).
    IF_T100_MESSAGE Interface Added to Message Class

    Figure 9.26    IF_T100_MESSAGE Interface Added to Message Class

  3. To use the message from the message class, go to the Texts tab, maintain the exception ID, and click the Message Text button, as shown in Figure 9.27.
    Maintaining Message from Message Class

    Figure 9.27    Maintaining Message from Message Class

  4. In the Assign Attributes of an Exception Class to a Message window, provide the Message Class name and message ID (Message Number). If you want to pass the attribute value to the message, you can select the attribute from the dropdown next to each attribute field. You can pass up to four attributes, as shown in Figure 9.28.
  5. For the attribute value to be shown in the message, the message in the message class should use & as a placeholder, as shown in Figure 9.29. When the message is called, the placeholder will be replaced with the actual value.
    Assigning Attributes to MessageMessagesassigning attributes

    Figure 9.28    Assigning Attributes to Message

    Using Placeholder in Message Maintained in Message Class

    Figure 9.29    Using Placeholder in Message Maintained in Message Class

With SAP NetWeaver 7.5, SAP introduced a new interface, IF_T100_DYN_MSG, which extends the functionality of the IF_T100_MESSAGE interface, which we discussed in the preceding steps. The IF_T100_DYN_MSG interface contains the IF_T100_MESSAGE interface as a component interface and contains the MSGTY attribute for the message type and attributes from MSGV1 to MSGV4 for the placeholders of the message.

Unlike using the IF_T100_MESSAGE interface, for which there is no built-in support for message attributes, with the IF_T100_DYN_MSG interface you don’t need to manually define attributes for dynamic messages, because they’re predefined in the interface. The IF_T100_DYN_MSG interface also provides built-in support to set the message type for the message using the MSGTY attribute.

9.4.3    Using the MESSAGE Addition to Raise an Exception

SAP NetWeaver 7.5 introduced a new addition, MESSAGE, which can be used when raising exceptions. Using the MESSAGE addition, you can pass the message specifications, such as the message type, message ID, and message number, as well as the placeholders for the message.

If the message class implements the IF_T100_DYN_MSG interface, you can pass the message type using the TYPE addition, and the placeholders for the message can be passed using the WITH addition (see Listing 9.9). However, if the message class implements the IF_T100_MESSAGE interface, then you can’t pass the message type and the message placeholders using the WITH addition.

Listing 9.9 shows example code to raise an exception when the IF_T100_DYN_MSG interface is implemented in the message class. In this example, we’re using the MESSAGE addition to pass the message specifications. The attributes and the message type can be passed directly without having to define them separately in the exception class, because the IF_T100_DYN_MSG interface provides built-in support.

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

Listing 9.9    Raising Exception with IF_T100_DYN_MSG Interface Implemented