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 module.... for beginner......

Former Member
0 Likes
2,491

Dear all experts,

I am new to ABAP.

Can anybody please tell, how to use the exceptions in function module ?

and if any exception happens, then how to link some messages to that exceptions?

I will also like to know, how to create those messages ?

<b>Can anybody please illustrate with help of example, so that a beginner will be able to understand. ?</b>

<b>eg,</b> i am adding two numbers in function module, and if any one passing number is negative, then i need to raise exception with message please do not enter -ve numbers for addition.

your help will be surely, rewarded with points.

waiting for reply.

Regards & Thanks

Vinay.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,352

after u call the FM, u need to write code for sy-subrc.

if sy-subrc = 1.

message e001(zz) with 'Exception 1'.

elseif sy-subrc = 2.

message e001(zz) with 'Exception 2'.

so...........on

endif.

4 REPLIES 4
Read only

Former Member
0 Likes
1,353

after u call the FM, u need to write code for sy-subrc.

if sy-subrc = 1.

message e001(zz) with 'Exception 1'.

elseif sy-subrc = 2.

message e001(zz) with 'Exception 2'.

so...........on

endif.

Read only

Former Member
0 Likes
1,352

Hi Vinay,

You can use the EXCEPTIONS option to handle the exceptions of the function module. If an exception e1, e2 … is raised while the function module is running, the system terminates the function module and does not pass any values from the function module to the program, except those that were passed by reference. If e1, e2 … is specified after the option EXCEPTION, the calling program handles the exception by assigning the value r1, r2 … to sy-subrc. You must specify r1, r2 … as a numeric literal.

If you specify of ERROR_MESSAGE in the EXCEPTION list you can influence the message handling of function modules. Normally, you should only call messages in function modules using the MESSAGE....RAISINGstatement. With ERROR_MESSAGE you can force the system to treat messages that are called without the RAISING option in a function module as follows:

· Messages of classes S, I, and W are ignored (but written to the log in a background job).

· Messages of classes E and A stop the function module as if the exception ERROR_MESSAGE had occurred (sy-subrc is set to rE).

If you specify OTHERS after EXCEPTIONS, the system assigns a single return code to all other exceptions that you have not specified explicitly in the list.

You can use the same number r1, r2 … for several exceptions.

Sample
You need to handle sy-subrc after calling the FM
if sy-subrc = 1.
message e001(yy) with 'Exception1'.
elseif sy-subrc = 2.
message e001(yy) with 'Exception2'.
elseif........
endif.

Just double click e001 to create message

Regds,
Younus

Reward Helpful Answers!!!

Read only

Former Member
0 Likes
1,352

Hi,

There are two ABAP statements for raising exceptions. They can only be used in function modules:

RAISE <except>.

and

MESSAGE..... RAISING <except>.

The effect of these statements depends on whether the calling program handles the exception or not. If the name <except> of the exception or OTHERS occurs in the EXCEPTIONS addition of the CALL FUNCTION statement, the exception is handled by the calling program.

If the calling program does not handle the exception

The RAISE statement terminates the program and switches to debugging mode.

The MESSAGE ..... RAISING statement display the specified message. How the processing continues depends on the message type.

If the calling program handles the exception, both statements return control to the program. No values are transferred. The MESSAGE ..... RAISING statement does not display a message. Instead, it fills the system fields SY-MSGID, SY-MSGTY, SY-MSGNO, and SY-MSGV1 to SY-MSGV4.

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

Regards

Sudheer

Read only

former_member673464
Active Contributor
0 Likes
1,352

hi..

Raising Exceptions

There are two ABAP statements for raising exceptions. They can only be used in function modules:

RAISE <except>.

and

MESSAGE..... RAISING <except>.

The effect of these statements depends on whether the calling program handles the exception or not. If the name <except> of the exception or OTHERS occurs in the EXCEPTIONS addition of the CALL FUNCTION statement, the exception is handled by the calling program.

If the calling program does not handle the exception

The RAISE statement terminates the program and switches to debugging mode.

The MESSAGE ..... RAISING statement display the specified message. How the processing continues depends on the message type.

If the calling program handles the exception, both statements return control to the program. No values are transferred. The MESSAGE ..... RAISING statement does not display a message. Instead, it fills the system fields SY-MSGID, SY-MSGTY, SY-MSGNO, and SY-MSGV1 to SY-MSGV4.

Source Code of READ_SPFLI_INTO_TABLE

The entire source code of READ_SPFLI_INTO_TABLE looks like this:

FUNCTION READ_SPFLI_INTO_TABLE.

*"----


""Local interface:

*" IMPORTING

*" VALUE(ID) LIKE SPFLI-CARRID DEFAULT 'LH '

*" EXPORTING

*" VALUE(ITAB) TYPE SPFLI_TAB

*" EXCEPTIONS

*" NOT_FOUND

*"----


SELECT * FROM SPFLI INTO TABLE ITAB WHERE CARRID = ID.

IF SY-SUBRC NE 0.

MESSAGE E007(AT) RAISING NOT_FOUND.

ENDIF.

ENDFUNCTION.

The function module reads all of the data from the database table SPFLI where the key field CARRID is equal to the import parameter ID and places the entries that it finds into the internal table SPFLI_TAB. If it cannot find any entries, the exception NOT_FOUND is triggered using MESSAGE...RAISING. Otherwise, the table is passed to the caller as an exporting parameter.

Also check these links

http://help.sap.com/search/highlightContent.jsp

http://help.sap.com/search/highlightContent.jsp

http://help.sap.com/search/highlightContent.jsp

regards,

veeresh