📄 ch20.htm
字号:
types <TT>e</TT>, <TT>w</TT>, and <TT>a</TT>, the function module
is exited and the calling program is terminated immediately. The
message is displayed and the output list appears blank. When the
user presses the Enter key, the blank list is removed and the
user is returned to the screen from which the program was invoked.
<LI>If the condition is not handled by the caller, and for message
type <TT>i</TT>, the message is displayed to the user in a dialog
box. When the user presses the Enter key, control returns to the
function module at the statement following the <TT>message</TT>
statement. The function module continues processing and, at the
<TT>endfunction</TT> statement, control returns to the calling
program. The values of the <TT>sy-msg</TT> variables are set and
the values of exports passed by value are returned.
<LI>If the condition is not handled by the caller, and for message
type <TT>s</TT>, the message is stored in a system area. Control
continues within the function module at the statement following
the <TT>message</TT> statement. The function module continues
processing and, at the <TT>endfunction</TT> statement, control
returns to the calling program. The values of the <TT>sy-msg</TT>
variables are set and the values of exports passed by value are
returned. When the list is displayed, the message appears in the
status bar at the bottom of the list.
<LI>If the condition is not handled by the caller, and for message
type <TT>x</TT>, the function module is exited and the calling
program is terminated immediately. A short dump is generated and
displayed to the user.
</UL>
<P>
Listings 20.9 and 20.10 illustrate the effect of the <TT>message
... raising</TT> statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 20.9 Using the MESSAGE ... RAISING Statement
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx2009.
2 parameters: p_etype default 'E', "enter message types E W I S A X
3 p_handle default 'Y'. "if Y, handle the exception
4 data vout(4) value 'INIT'.
5
6 if p_handle = 'Y'.
7 perform call_and_handle_exception.
8 else.
9 perform call_and_dont_handle_exception.
10 endif.
11 write: / 'sy-subrc =', sy-subrc,
12 / 'vout =', vout,
13 / 'sy-msgty', sy-msgty,
14 / 'sy-msgid', sy-msgid,
15 / 'sy-msgno', sy-msgno,
16 / 'sy-msgv1', sy-msgv1.
17
18 *_____________________________________________________________________
19 form call_and_handle_exception.
20 call function 'Z_TX_2010'
21 exporting
22 msgtype = p_etype
23 importing
24 pout = vout
25 exceptions
26 error_e = 1
27 error_w = 2
28 error_i = 3
29 error_s = 4
30 error_a = 5
31 error_x = 6
32 others = 7.
33 endform.
34
35 *_____________________________________________________________________
36 form call_and_dont_handle_exception.
37 call function 'Z_TX_2010'
38 exporting
39 msgtype = p_etype
40 importing
41 pout = vout.
42 endform.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 20.10 The Source Code for the Function Module
Called from Listing 20.9<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 function z_tx_2010.
2 *"------------------------------------------------------------
3 *"*"Local interface:
4 *" IMPORTING
5 *" VALUE(MSGTYPE)
6 *" EXPORTING
7 *" VALUE(POUT)
8 *" EXCEPTIONS
9 *" ERROR_E
10 *" ERROR_W
11 *" ERROR_I
12 *" ERROR_S
13 *" ERROR_A
14 *" ERROR_X
15 *"------------------------------------------------------------
16 pout = 'XXX'.
17 case msgtype.
18 when 'E'. message e991(zz) with 'Error E' raising error_e.
19 when 'W'. message e992(zz) with 'Error W' raising error_w.
20 when 'I'. message e993(zz) with 'Error I' raising error_i.
21 when 'S'. message e994(zz) with 'Error S' raising error_s.
22 when 'A'. message e995(zz) with 'Error A' raising error_a.
23 when 'X'. message e996(zz) with 'Error X' raising error_x.
24 endcase.
25 endfunction.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
Using the default parameter values, the code in Listings 20.9
and 20.10 produce this output:
<BLOCKQUOTE>
<PRE>
sy-subrc = 1
vout = INIT
sy-msgty E
sy-msgid ZZ
sy-msgno 991
sy-msgv1 Error E
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>In Listing 20.9, when using the default values for the parameters
line 6 is true, so control transfers to the subroutine <TT>call_and_handle_exception</TT>.
<LI>Line 20 transfers control to line 1 of Listing 20.10.
<LI>In Listing 20.10, line 16 assigns a value to the local parameter
<TT>pout</TT>. This value is defined as pass-by-value, so the
value of <TT>vout</TT> in the caller is not yet changed.
<LI>Line 18 of the <TT>case</TT> statement is true and the <TT>message
... raising</TT> statement is executed. This triggers the exception
<TT>error_e</TT>.
<LI>The exception <TT>error_e</TT> is handled by the calling program,
so the message is not displayed to the user. Control returns to
line 25 of Listing 20.9. The value of <TT>vout</TT> is not changed;
the <TT>sy-msg</TT> variables are assigned values.
<LI>In Listing 20.9, line 26 assigns <TT>1</TT> to <TT>sy-subrc</TT>.
<LI>Line 33 returns control to line 11 and the values are written
out.
</UL>
<P>
Try running program <TT>ztx2009</TT>. By specifying a <TT>p_handle</TT>
value of <TT>N</TT>, you will see error messages produced.
<H3><A NAME="DefiningExceptionsintheInterface">
Defining Exceptions in the Interface</A></H3>
<P>
Whenever you raise an exception in a function module, you should
document that name in the function module's interface. The exception
names will also automatically appear at the top of the function
module source within the system-generated comments. The following
procedure describes how to document your exceptions.
<P>
<img src="../button/screencam.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/screencam.gif">
<P>
Start the ScreenCam "How to Document Exceptions in the Function
Module Interface" now.
<OL>
<LI>Begin at the Function Library: Initial Screen.
<LI>Type your function module name in the Function Module field
if it is not already there.
<LI>Choose the Table Parameters/Exceptions Interface radio button.
<LI>Press the Change pushbutton. The Function Module Change: Table
Parameters/Exceptions screen is displayed.
<LI>In the bottom half of the screen, in the Exceptions section,
type the names of the exceptions that appear in the source code
of the function module.
<LI>Press the Save button and the Back button. You are returned
to the Function Library: Initial Screen.
</OL>
<H2><A NAME="AutomaticallyInsertingthecallfunctionStatement"><FONT SIZE=5 COLOR=#FF0000>
Automatically Inserting the call function Statement</FONT></A></H2>
<P>
Instead of typing the <TT>call function</TT> statement into your
source code, the system can automatically generate it for you.
When you do it this way, the exception names that you documented
in the interface will automatically be inserted into your code
as well.
<P>
<img src="../button/screencam.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/screencam.gif">
<P>
Start the ScreenCam "How to Insert a <TT>call function</TT>
Statement into Your ABAP/4 Program" now.
<P>
To see the effects of your exception documentation, create an
ABAP/4 program. Instead of coding the <TT>call function</TT> statement
yourself, use the following procedure to automatically generate
the <TT>call function</TT> statement:
<OL>
<LI>Begin at the ABAP/4 Editor: Edit Program screen.
<LI>Position your cursor within the source code on the line after
the one where you want the <TT>call function</TT> statement to
appear.
<LI>Press the Pattern button on the Application toolbar. The Insert
Statement dialog box appears.
<LI>The Call Function radio button should already be chosen. If
it isn't, select it.
<LI>Type the name of your function module in the input field to
the right of Call Function.
<LI>Press the Continue button (the green checkmark). You are returned
to the Edit Program screen. A <TT>call function</TT> statement
will appear in the source code on the line above the one you positioned
your cursor on. The statement will begin in the same column as
the column that your cursor was in. The parameters and exceptions
that you named in the interface will appear. If a parameter is
marked as optional in the interface, it will be commented out
and the default value will appear to the right of it.
<LI>Fill in the values to the right of the equal signs. Feel free
to change the code or remove any lines that follow the <TT>importing</TT>
addition if you do not need those return values.
</OL>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>CAUTION</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
An empty <TT>importing </TT>clause causes a syntax error. If there are no parameter names following the word <TT>importing</TT>, or if they are all commented out, you must remove the word <TT>importing</TT>. The same is also true for <TT>exporting</TT>.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<UL>
<LI>Global variables can be defined at two levels. When defined
in the top <TT>include</TT>, they are global to all function modules
within the group and remember their values between calls to function
modules within the group as long as the calling program continues
to execute. By globalizing the interface, parameters are visible
within subroutines called from the function module.
<LI>Subroutines defined within a function group are coded in the
F01 include.
<LI>Function modules can be released to denote that the interface
is stable and no significant changes will be made to it. It is
therefore released for general availability and is "safe"
to use in production code.
<LI>The test environment provides a convenient way of running
function modules without writing any ABAP/4 code.
<LI>Existing function modules can be found with the aid of the
Repository Information System.
</UL>
<H2><A NAME="QampABR"><FONT SIZE=5 COLOR=#FF0000>
Q&A<BR>
</FONT></A></H2>
<TABLE>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>Why would you pass an import parameter by reference? If you specify both import and export parameters should be passed by reference, aren't they the same? Why does the ability to do this exist?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>A pass by reference is more efficient than a pass by value. You should use it for efficiency. However, the words Import and Export provide important documentation regarding the role each parameter plays within the function module. You should not change Import parameters, they should always be passed in without being changed by the function module. Nor should you accept values into the function module via Export parameters. The value of an Export should always originate within the function module.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><FONT SIZE=5 COLOR=#FF0000>
Workshop</FONT></A></H2>
<P>
The Workshop provides you two ways for you to affirm what you've
learned in this chapter. The Quiz section poses questions to help
you solidify your understanding of the material covered and the
Exercise section provides you with experience in using what you
have learned. You can find answers to the quiz questions and exercises
in Appendix B, "Answers to Quiz Questions and Exercises."
<H3><A NAME="Quiz">
Quiz</A></H3>
<OL>
<LI>If a parameter with the same name exists in two function modules
within a group and both interfaces are global, must the data definitions
of both of these parameters be identical?
<LI>If you accidentally release a function module and want to
cancel the release, what menu path can you use?
<LI>If you do not code <TT>others</TT> and an exception is raised
within the function module that is not named on the <TT>exceptions</TT>
addition, what happens?
<LI><TT>Check</TT>, <TT>exit</TT>, and <TT>stop</TT> have the
same effect with function modules as they do in external subroutines.
Should they be used within function modules?
</OL>
<H3><A NAME="Exercise">
Exercise 1</A></H3>
<P>
Copy the <TT>z_tx_div</TT> function module, and modify it to raise
the <TT>zero_divide</TT> exception if the value of <TT>p2</TT>
is zero. (To copy a function module, use the Copy button on the
Function Libr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -