Вы находитесь на странице: 1из 12

Message Class

Messages
Messages provide another way to communicate with the user, in addition to general
screens and selection screens. They are mostly used for error handling during the
processing of other user dialogs.

Message Management
Messages are single texts, stored in table T100 that you can maintain in Transaction
SE91 or by forward navigation in the ABAP Workbench. T100 has the following
structure:

Language key

Twenty-character message class

Message number

Message text (up to 72 characters)

Work areas assign messages to an application (maybe to a package, for example),


and the message numbers identify the individual messages within the work area.
When you call a message in ABAP, you need to specify the work area and message
number. The language key is supplied by the system according to the system
language environment.

Messages
You send messages in the logon language of the current user using the ABAP
statement MESSAGE. The statement specifies the message class, number, and type
of the message.
The message class and number are used to identify the message in table T100. The
message type is one of A, E, I, S, W, or X, and defines how the ABAP runtime should
process the message.
Messages can either be displayed in modal dialog boxes, or in the status bar of the
screen. How a message is processed depends on its type and on the context in which
the message is sent.

The MESSAGE Statement


The MESSAGE statement has three variants and several additions:

Using a Global Message Class


If the introductory statement of a program contains the addition
... MESSAGE-ID id.
and a message class id contained in table T100, you can use the MESSAGE
statement as follows:
MESSAGE tnnn [WITH f1 ... f4] [RAISING exc].
where t is the single-character message type and nnn the three-digit message
number. The system retrieves the corresponding message text from table T100 for
the message class specified in the introductory statement, and displays it. The
display depends on the message type and the context in which the message is sent.

Specifying the Message Statically


To specify the message class, message number, and message type statically, use the
following form of the MESSAGE statement:
MESSAGE tnnn(id) [WITH f1 ... f4] [RAISING exc].
This statement is like the first variant, but here you specify the message class id
within the MESSAGE statement. This specification overrides any message class that
you may have specified at the beginning of the program.

Specifying the Message Dynamically


To specify the message class, message number, and message type dynamically, use
the following form of the MESSAGE statement:

MESSAGE ID id TYPE t NUMBER n [WITH f1 ... f4] [RAISING exc].

where id, t and n are fields containing the message class, message number, and
message type respectively. The system uses the field contents to read the

appropriate message from table T100 and displays it according to the message
context.

Filling Message Texts Dynamically


Message texts in table T100 can contain up to four ampersand characters (&) as
placeholders. You can replace these at runtime using the WITH addition in the
MESSAGE statement:
MESSAGE ... WITH f1 ... f4.
The contents of fields f1 ... f4 are then inserted sequentially into the message text in
place of the placeholders.

Messages and Exceptions


Within function modules and methods, you can use the RAISING addition in the
MESSAGE statement to trigger exception:
MESSAGE ... RAISING exc.
If the calling program does not handle the exception exc itself, the message is
displayed, and the program continues processing in the manner appropriate to the
message type and context. If the calling program handles the exception, the
message is not displayed, but the exception is triggered. In this case, the message
class, message number, message type, and any values of placeholders are placed in
the system fields sy-msgid, sy-msgno, sy-msgty and sy-msgv1 to sy-msgv4 in
the calling program.
Example
REPORT demo_messages_simple MESSAGE-ID sabapdocu.
MESSAGE i014.
MESSAGE s015.
WRITE text-001.
This simple message displays an information message in a modal dialog
box:

and a success message in the status bar of the next screen (in this case, a list).

Message Processing
Message processing depends on the message type specified in the MESSAGE
statement, and the program context in which the statement occurs.
Message Types
A

E
I

W
X

Termination
Message

The message appears in a dialog box, and the program


terminates. When the user has confirmed the message,
control returns to the next-highest area menu.

Error Message

Depending on the program context, an error dialog


appears or the program terminates.

Information

The message appears in a dialog box. Once the user has


confirmed the message, the program continues immediately
after the MESSAGE statement.

Status Message

The program continues normally after the MESSAGE


statement, and the message is displayed in the status bar of
the next screen.

Warning

Depending on the program context, an error dialog


appears or the program terminates.

Exit

No message is displayed, and the program terminates with a


short dump. Program terminations with a short dump
normally only occur when a runtime error occurs. Message
type X allows you to force a program termination. The short
dump contains the message ID.

Contexts
Messages, especially those with type E or W, are processed according to the context
in which they occur.
Messages without Screens
This context applies to all situations that do not belong to any screen processing. In
ABAP programs, this includes the following processing blocks:
1

The program constructor LOAD-OF-PROGRAM

PBO modules (PBO of screens)

The selection screen event AT SELECTION-SCREEN OUTPUT (PBO of a selection


screen)

The reporting events INITIALIZATION, START-OF-SELECTION, GET, and ENDOF-SELECTION

The list events TOP-OF-PAGE and END-OF-PAGE

All other processing blocks are associated with screen processing (reacting to user
input).

In order to make the following table more concise, the above mentioned processing
blocks are structured into two groups.
Group 1: LOAD-OF-PROGRAM, PBO module of screens, AT SELECTION-SCREEN
OUTPUT
Group 2: Reporting and list events (INITIALIZATION, START-OF-SELECTION, GET,
END-OF-SELECTION, TOP-OF-PAGE and END-OF-PAGE)

Message Processing
Typ

Display

Processing

e
A

Program terminates, and control returns to

Dialog box

last area menu


Group 1: as type A.

Group 1: Dialog box

Group 2: Program termination and display of

Group 2: Status line of

an empty screen with empty GUI status.

current window

After the user action: Return to the calling


position of the program.

Group 1: Status line of the

Program continues processing after the

..............next screen

MESSAGE statement

Group 2: Dialog box


S

Status line of next screen


Group 1: Status line of the

..............next screen
Group 2: Status line of the
...............current window

None

Program continues processing after the


MESSAGE statement
Group 1: as type S.
Group 2: Program termination and display of
an empty screen with empty GUI status.
After the user action: Return to the calling
position of the program.
Triggers a runtime error with short dump

Messages on Screens
This context includes all situations where a screen is being processed, that is, the
program is reacting to user input. In ABAP programs, this means all PAI modules.
Message Processing
Typ

Display

Processing

Dialog box

Program terminates, and control returns to last

e
A

area menu
E

Status bar

PAI processing is terminated, and control returns


to the current screen. All of the screen fields for
which there is a FIELD or CHAIN statement are
ready for input. The user must enter a new
value. The system then restarts PAI processing
for the screen using the new values. Error
messages are not possible in POH or POV
processing. Instead, a runtime error occurs.

Dialog box

Program

continues

processing

after

the

processing

after

the

MESSAGE statement
S

Status bar of next screen

Program

continues

MESSAGE statement
W

Status bar

Like type E, but the user can confirm the


message by pressing ENTER without having to
enter new values. The system then resumes PAI
processing

directly

after

the

MESSAGE

statement. Warning messages are not possible


in POH or POV processing. Instead, a runtime
error occurs.
X

None

Triggers a runtime error with short dump

Messages on Selection Screens


This context includes all situations where a selection screen is being processed, that
is, the program is reacting to user input. In ABAP programs, this corresponds to the
AT SELECTION-SCREEN processing blocks, except those with the OUTPUT addition.
Message Processing
Typ

Display

Processing

Dialog box

Program terminates, and control returns to last

e
A

area menu
E

Status line

Selection screen processing terminates, and the


selection screen is redisplayed. The screen fields
specified through the additions to the

AT

SELECTION-SCREEN statement are ready for


input. The user must enter a new value. The
system

then

restarts

the

selection

screen

processing using the new values. You cannot use


error messages with the ON HELP-REQUEST ,
ON VALUE-REQUEST and ON EXIT additions.
Instead, a runtime error occurs.
I

Dialog box

Program

continues

processing

after

the

processing

after

the

MESSAGE statement
S

Status line of next screen

Program

continues

MESSAGE statement
W

Status line

Like type E, but the user can confirm the


message by pressing Enter without having to
enter new values. The system then resumes
selection screen processing directly after the
MESSAGE statement. You cannot use warnings
with the ON HELP-REQUEST , ON VALUEREQUEST and ON EXIT additions. Instead, a
runtime error occurs.

None

Triggers a runtime error with short dump

Messages in Lists
This context includes all situations where a list is being processed, that is, the
program is reacting to user interaction with lists. In ABAP programs, this includes the
following processing blocks:
1

AT LINE-SELECTION

AT USER-COMMAND

AT PFnn

TOP-OF-PAGE DURING LINE-SELECTION


Message Processing

Typ
e

Display

Processing

Dialog box

Program terminates, and control returns to last


area menu

Status bar

Processing block terminates. Previous list levels


remain displayed.

Dialog box

Program continues processing after the


MESSAGE statement

Status bar of next screen

Program continues processing after the


MESSAGE statement

Status bar

Like type E

None

Triggers a runtime error with short dump

Messages in Function Modules and Methods


Messages have two different functions in function modules and methods:
Normal Messages
If you use messages in function modules and methods without the RAISING addition
in the MESSAGE statement, and the caller does not catch the message, the message
is handled normally according to the context in which it is called within the function
module or method.
Triggering Exceptions
If you use messages in function modules and methods with the addition
... RAISING exc
in the MESSAGE statement, the way in which the message is handled depends on
whether the calling program handles the exception exc or not.

If the calling program does not handle the exception, the message is displayed
and handled according to the context in which it occurs in the function module or
method from which it was called.

If the calling program handles the exception, the message is not displayed.
Instead, the procedure is interrupted in accordance with the message type, and
processing returns to the calling program. The contents of the message are
placed in the system fields sy-msgid, sy-msgty, sy-msgno, and sy-msgv1
through sy-msgv4 .

Catching Messages in the Calling Program


If messages are sent in function modules without the addition RAISING of the
MESSAGE statement, it is possible to influence message handling through
specification of the implicit exception error_message in the EXCEPTION list of the
statement CALL FUNCTION. If error_message is specified, the following conditions
apply:
1

Type S, I, and W messages are ignored (but logged during background


processing)

Type E and A messages trigger the exception error_message

Type X messages trigger the usual runtime error and short dump.

Catching messages is not currently supported for methods.

Maintaining Messages
Messages allow you to communicate with users from your programs. They are mainly
used when the user has made an invalid entry on a screen.
To send messages from a program, you must link it to a message class. Each
message class has an ID, and usually contains a whole set of message.
Each message has a single line of text, and may contain placeholders for variables.
All messages are stored in table T100. You create and edit them using Transaction
SE91. Once you have created a message, you can use it in the MESSAGE statement
in a program.

Starting the Message Maintenance Transaction

Using forward navigation from the ABAP Editor.

You can display the messages for your program from the ABAP Editor by
choosing Goto Messages. The Maintain Messages screen appears. By
default, the system display the message class linked to the current program.

You can also enter Transaction SE91.

If you choose Goto Messages from the ABAP Editor and your program does not
have a defined message class, the system assumes you want to browse an existing
class and prompts you for a message class ID.
Creating Message Classes
Procedure
To create a new message class from the ABAP Editor:
1. In the initial statement (for example, REPORT) or directly in the statement
MESSAGE ID <id> enter a parameter value of up to 20 characters as a
message ID, for example:
REPORT <name> MESSAGE-ID <message class>.
Messages are stored system-wide. The chosen message ID (message class)
must not already exist in the system.
2. Double-click the message ID.
If you enter an ID that already exists in the system, the system calls up
Message maintenance. In this case, enter a different ID. Once you have done
this, the system asks you whether you want to create a new message class.
3. Choose Yes.
The system calls up Message maintenance.
4. Enter a brief description of the class in Short text.
5. Choose Save.
Result

You can add messages to your class by choosing the Messages tab. You can always
return to the Message maintenance screen by double-clicking a message ID or a
message number where it appears in your code.
Adding Messages
Assuming that a valid message class exists in the ABAP program:
1. Choose the menu path Goto Messages
The system displays a list of all messages belonging to the message class.
2. Select

to switch to change mode.

All lines are ready for input.


3. Enter the message text (short text) for the next free number.

4. If the text is self-explanatory click the corresponding checkbox.


Note that you cannot create a long text for self-explanatory messages.
Choose Save.
Using forward navigation you can also add a new message to the message class. To
do this enter a number for the new message in the MESSAGE statement and doubleclick it. You are advised to choose the next free number for your new message.

Вам также может понравиться