Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Exceptions in Function Modules

Former Member
0 Likes
2,824

Hi

How can I create an Exception in a Function Module and Raise that based on some condition? Plz help

9 REPLIES 9
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,616

Under SE37 go to the exception tab and give names to the exception.

Then in the source tab, use statement like RAISE <exception name> or MESSAGE Ennn(xxx) RAISING <exception name> to raise the error.

Look at [Understanding Function Module Code|http://help.sap.com/saphelp_47x200/helpdata/en/d1/801f1c454211d189710000e8322d00/frameset.htm]

Regards

Read only

0 Likes
1,616

How can I define what message to be shown when an exception happens!???

Read only

0 Likes
1,616

If you use the MESSAGE ... RAISING syntax, choose standard or create specific message (SE91, or SE16 on table T100)

In the calling program, you can also choice a message, and display it when the exception is received (assigned to a SY-SUBRC in the CALL FUNCTION EXCEPTION statement)

Regards

Read only

Former Member
0 Likes
1,616

In the exception Tab of SE37 place/add the exceptions which ever you want.

and in the coding part according to the condition raise them.

Read only

Former Member
0 Likes
1,616

Go to the exception tab in the function module. Give the names of the exceptions with short text. Then Save and activate the FM. In side of the source code of that FM write

DATA RCODE LIKE SONV-RCODE.

Now after any occurence of exeption

write

Raise exception name and give rcode = exception number.

Now inside of the calling program

just call the function module.

Give points if satisfied.

Read only

Former Member
0 Likes
1,616

Hi Santo,,,,

Look into this link ,, where u will get a clear information on exceptions,,,,,

http://help.sap.com/saphelp_nw70/helpdata/en/9f/db98fc35c111d1829f0000e829fbfe/content.htm

http://abapprogramming.blogspot.com/2007/06/lesson-24-function-groups-and-function.html

In the function module, you can create your own local types and data objects, and call subroutines or other function modules.

You can make a function module trigger exceptions .

To do this, you must first declare the exceptions in the interface definition, that is, assign each one a different name.

In the source code of your function module, you program the statements that trigger an exception under the required condition. At runtime, the function module is terminated when an exception is triggered.

The changes to exporting and changing parameters are the same as in subroutines. There are two statements that you can use to trigger an exception. In the forms given below, stands for the name of an exception that you declared in the interface. The system reacts differently according to whether or not the exception was listed in the function module call:

RAISE .

If the exception is listed in the calling program, the system returns control to it directly. If the exception is not listed, a runtime error occurs.

MESSAGE () RAISING .

If the exception is listed in the calling program, the statement has the same effect as RAISE . If it is not listed, the system sends message from message class with type , and no runtime error occurs.

Function modules differ from subroutines in that you must assume that they will be used by other programmers. For this reason, you should ensure that you complete the steps listed here.

Documentation (can be translated)

You should document both your parameters and exceptions with short texts (and long texts if necessary) and your entire function module. The system provides a text editor for you to do this, containing predefined sections for Functionality, Example Call, Hints, and Further Information.

Work list

When you change an active function module, it acquires the status active (revised). When you save it, another version is created with the status inactive . When you are working on a function module, you can switch between the inactive version and the last version that you activated. When you activate the inactive version, the previous active version is overwritten.

Function test

Once you have activated your function module, you can test it using the built-in test environment in the Function Builder. If an exception is triggered, the test environment displays it, along with any message that you may have specified for it. You can also switch into the Debugger and the Runtime Analysis tool. You can save test data and compare sets of results.

When you insert a function module call in your program, you should use the Pattern function. Then, you only need to enter the name of the function module (input help is available). The system then inserts the call and the exception handling (MESSAGE statement) into your program.

You assign parameters by name. The formal parameters are always on the left-hand side of the expressions:

Exporting parameters are passed by the program. If a parameter is optional, you do not need to pass it. Default values are displayed if they exist.

Importing parameters are received by the program. All importing parameters are optional.

Changing parameters are both passed and received. You do not have to list optional parameters.

Default values are displayed if they exist.

The system assigns a value to each exception, beginning at one, and continuing to number them sequentially in the order they are declared in the function module definition. You can assign a value to all other exceptions that you have not specifically listed using the special exception OTHERS.

If you list the exceptions and one is triggered in the function module, the corresponding value is placed in the return code field sy-subrc. If you did not list the exception in the function call, a runtime error or a message occurs, depending on the statement you used in the function module to trigger the exception.

When you create a function module, you must assign it to a function group. The function group is the main program in which a function module is embedded.

A function group is a program with type F, and is not executable . The entire function group is loaded in a program the first time that you call a function module that belongs to it.

The system also triggers the LOAD-OF-PROGRAM event for the function group.

The function group remains active in the background until the end of the calling program. It is therefore a suitable means of retaining data objects for the entire duration of a program. All of the function modules in a group can access the group's global data.

Please reward if found helpful,,,,,,,,,,,,,,,,

Thanks And Regards ,,,,

Sreekar.Kadiri.

Read only

Former Member
0 Likes
1,616

You can just declare exceptions in the exception tab and in the source code you can raise this exception using RAISE statement.

For eg:

IF SY-SUBRC ne 0.

Raise <Exception>.

ENDIF.

Read only

Former Member
0 Likes
1,616

Tx

Read only

Former Member
0 Likes
1,616

Tx