0% found this document useful (0 votes)
168 views5 pages

Exception Class To Use Messages From T100

The document discusses how to define an exception class in ABAP to use messages from the message repository T100. It explains how to include the IF_T100_MESSAGE interface to access message parameters, and how to define exception IDs with message class, number, and attributes in the exception class. It provides an example of raising and catching an exception, retrieving message attributes at catch time. It cautions against having too many exception IDs without logical grouping into classes.

Uploaded by

Anjan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views5 pages

Exception Class To Use Messages From T100

The document discusses how to define an exception class in ABAP to use messages from the message repository T100. It explains how to include the IF_T100_MESSAGE interface to access message parameters, and how to define exception IDs with message class, number, and attributes in the exception class. It provides an example of raising and catching an exception, retrieving message attributes at catch time. It cautions against having too many exception IDs without logical grouping into classes.

Uploaded by

Anjan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exception class to use Messages

from T100
By Naimesh Patel | April 2, 2013 | Exceptions | 26,581 | 13

Exception class can be defined using to use static Text as well as the messages from the
Message repository maintained using transaction SE91. In this article, lets see how to use
the message from Message repository T100.

Define Exception Class using


IF_T100_MESSAGE
To achieve and use the messages from the message repository, you need to include the
interface IF_T100_MESSAGE in the exception class. This interface allows you to declare and
pass the message parameters when raising the exception.

As soon as you add the interface, the parameters of the method CONSTRUCTOR would be
adjusted. The TEXTID field is not more referring to the field TEXTID. It would now refer to
the type of the field IF_T100_MESSAGE=>T100KEY. So, when you raise an exception, you
can simply populate the fields of the key and they would be propagated back to the place
where exception would be caught.

Interface IF_T100_MESSAGE has attribute T100KEY which would be available at time of


catching the exception to retrieve the passed message id, message number and message
parameters when exception was raised.

Exception ID in the Exception Class


For each Exception ID, in the Exception class, you can assign message class, message
number and 4 attributes. To be able to assign one these 4 attributes, you would need to
create a public attribute in the exception class. Once the attribute is available, you can
select that instance attribute in the exception ID message text. E.g. As shown in the image, I
have created an attribute IV_FIELD1 which as an attribute in the class which I have
assigned to the attribute when I set the message text for the exception ID.

This text ID would be created as a Constant in the Exception Class as well.

Exception ID Constant

CONSTANTS:
BEGIN OF ZCX_MSG_T100,
msgid TYPE symsgid VALUE '00',
msgno TYPE symsgno VALUE '398',
attr1 TYPE scx_attrname VALUE 'IV_FIELD1',
attr2 TYPE scx_attrname VALUE ",
attr3 TYPE scx_attrname VALUE ",
attr4 TYPE scx_attrname VALUE ",
END OF ZCX_MSG_T100 .

The added public attribute, which you have used is also available as one of the parameter
in the CONSTRUCTOR. This would let you assign a specific value to that attribute while
raising the exception. Thus you can propagate that value
back the catcher of the exception.
Complete Public section of the Exception Class looks like this:

Exception Class Public section

CLASS ZCX_MSG_T100 DEFINITION


PUBLIC
INHERITING FROM CX_NO_CHECK
FINAL
CREATE PUBLIC .

*"* public components of class ZCX_MSG_T100


*"* do not include other source files here!!!
PUBLIC SECTION.

INTERFACES IF_T100_MESSAGE .

CONSTANTS:
BEGIN OF ZCX_MSG_T100,
msgid TYPE symsgid VALUE '00',
msgno TYPE symsgno VALUE '398',
attr1 TYPE scx_attrname VALUE 'IV_FIELD1',
attr2 TYPE scx_attrname VALUE ",
attr3 TYPE scx_attrname VALUE ",
attr4 TYPE scx_attrname VALUE ",
END OF ZCX_MSG_T100 .
DATA IV_FIELD1 TYPE CHAR10 .

METHODS CONSTRUCTOR
IMPORTING
!TEXTID LIKE IF_T100_MESSAGE=>T100KEY OPTIONAL
!PREVIOUS LIKE PREVIOUS OPTIONAL
!IV_FIELD1 TYPE CHAR10 OPTIONAL .

Raising & Catching Exception


While raising the exception, you can provide all the parameters which are available in the
CONSTRUCTOR method. This would than set proper attributes, which would be available
at time of catching the exception. When proper parameters are set, it would provide proper
information.

Demo
A small Demo to see it in action:

Program to Raise and Catch exception

*&---------------------------------------------------------------------*
*& Purpose - Object Oriented Implementation for a Report
*& Author - Naimesh Patel
*& URL - http://zevolving.com/?p=2040
*&---------------------------------------------------------------------*
REPORT ZTEST_NP_T100_EXCEPTION.
*
DATA: lo_exc TYPE REF TO zcx_msg_t100.
DATA: ls_t100_key TYPE scx_t100key.
*
START-OF-SELECTION.
* no message id, would take default ID
* No additional parameter, message would be empty
TRY.
RAISE EXCEPTION TYPE zcx_msg_t100.
CATCH zcx_msg_t100 INTO lo_exc.
MESSAGE lo_exc TYPE 'I'.
ENDTRY.
*
* Specific message ID, with parameter set
TRY.
RAISE EXCEPTION TYPE zcx_msg_t100
EXPORTING
textid = zcx_msg_t100=>ZCX_MSG_T100
IV_FIELD1 = 'Value1'.
CATCH zcx_msg_t100 INTO lo_exc.
MESSAGE lo_exc TYPE 'I'.
ENDTRY.
*
* Raising with a different message
TRY.
ls_t100_key-msgid = '00'.
ls_t100_key-msgno = '443'.
RAISE EXCEPTION TYPE zcx_msg_t100
EXPORTING
textid = ls_t100_key.
CATCH zcx_msg_t100 INTO lo_exc.
MESSAGE lo_exc TYPE 'I'.

* getting all the attributes of the message


* like ID, class, attr1 etc
MESSAGE ID lo_exc->IF_T100_MESSAGE~T100KEY-msgid
TYPE 'I'
NUMBER lo_exc->IF_T100_MESSAGE~T100KEY-msgno
WITH lo_exc->IF_T100_MESSAGE~T100KEY-ATTR1.
ENDTRY.

Multiple Exception IDs Word of Caution


You would think this is great way to reduce the number of Exception Classes by creating
many different exception IDs. When you have many exception IDs, they would need many
different attributes. These attributes than would be added to CONSTRUCTOR. If you keep
on adding various different attributes which are used in your Exception IDs, your
Constructor method signature would grow. Over the time, it would be difficult for Other
developers to know which fields are only needed when correctly raising the exception. Thus
exception IDs should be logically grouped into Exception Classes.

Tags

You might also like