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

how to write the exceptions in function module

Former Member
0 Likes
11,169

dear all,

how to write the exceptions in function modules with example.

thanq

jyothi

7 REPLIES 7
Read only

Former Member
0 Likes
2,604

Hi,

In FM exceptions you can define your exceptions with numbers.

when the exception araised the Sy-subrc will consist the Number that you are already defined in the Function module.

Check the following Example :

CALL FUNCTION 'READ_SPFLI_INTO_TABLE'

EXPORTING

ID = CARRIER

IMPORTING

ITAB = JTAB

EXCEPTIONS

NOT_FOUND = 1

OTHERS = 2.

CASE SY-SUBRC.

WHEN 1.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO.

WHEN 2.

MESSAGE E702(AT).

ENDCASE.

Thanks.

Edited by: Viji on Mar 27, 2008 9:21 AM

Read only

Former Member
0 Likes
2,604

Hi,

In Tcode SE37 __-->change mode -->click on exception tab

-


>enter exception name and short description

Fro Ex :

Exception Short description

ARGUMENT_ERROR Incorrect call

In your code according to login raise exception

RAISE ARGUMENT_ERROR.

Read only

Former Member
0 Likes
2,604

in the exceptions tab, list your exceptions with their description

eg :

DATA_NOT_FOUND no data found

in the source code, raise the exception as follows

RAISE DATA_NOT_FOUND

while calling the fm, if the exception is raised, the value of the sy-subrc will be set according to the position of the exception in the list in the exception tab. if it is at the first position, sy-subrc will be set to 1.

Read only

Former Member
0 Likes
2,604

HI,

In function module Exception one option is there click that option

Give Exption name and Short text like this,

Refrence_not_allowed  <Give Short text>

In source code

IF REFERENCE = TRUE.
    RAISE REFERENCE_NOT_ALLOWED.
  ENDIF.

Read only

Former Member
0 Likes
2,604

Hi,

Raising Exceptions

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

RAISE except.

und

MESSAGE.....RAISING except.

The effect of these statements depends on whether the calling program handles the exception or not. The calling program handles an exception If the name of the except exception or OTHERS is specified after the EXCEPTION option of the CALL FUNCTION statement.

If the calling program does not handle the exception

· The RAISEstatement terminates the program and switches to debugging mode.

· The MESSAGE..... RAISING statement displays the specified message. Processing is continued in relation to 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 with MESSAGE ... RAISING. Otherwise, the table is passed to the caller as an exporting parameter.

Calling READ_SPFLI_INTO_TABLE

The following program calls the function module READ_SPFLI_INTO_TABLE:

REPORT demo_mod_tech_fb_read_spfli.

PARAMETERS carrier TYPE s_carr_id.

DATA: jtab TYPE spfli_tab,

wa LIKE LINE OF jtab.

CALL FUNCTION 'READ_SPFLI_INTO_TABLE'

EXPORTING

id = carrier

IMPORTING

itab = jtab

EXCEPTIONS

not_found = 1

OTHERS = 2.

CASE sy-subrc.

WHEN 1.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno.

WHEN 2.

MESSAGE e702(at).

ENDCASE.

LOOP AT jtab INTO wa.

WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.

ENDLOOP.

The actual parameters carrier and jtab have the same data types as their corresponding interface parameters in the function module. The exception NOT_FOUND is handled in the program. It displays the same message that the function module would have displayed had it handled the error.

Or

just have to decide what exceptions u want and under what conditions.

then declarethese exeptions under the exceptions tab.

in the source code of ur function module.

if

.

.

.

like this u can code .

now when u call the function module in tme mainprogram.

if some error occurs and u have declared a exception for this then it will set sy-subrc = value u give inthe call of this fm.

in the fm u can program these sy-subrc values and trigger the code for ur exception.

Please reward if useful

Regards,

Ravi

Edited by: Ravikanth Alapati on Mar 27, 2008 9:36 AM

Read only

Former Member
0 Likes
2,604

Hi,

Define the whatever exceptions to be raised in Exceptions tab in Function module.

In Source code, Raise those exceptions depending on ur conditions.

Automatically those exceptions will be coming to the main program to handle if any error occurs.

Check Sy-subrc in the main program.

If sy-subrc = 1. ( first Exception in Function Module)

Write:/ ' text to understand'.

if sy-subrc = 2. ( second Exception in Function Module if u've)

write:/ ' text to understand'.

Reward if helpful.

Iyswarya

Edited by: Iyswarya Godi on Mar 27, 2008 9:27 AM

Read only

Former Member
0 Likes
2,604

Hi,

Exceptions in Function Module

Exceptions are used to handle error scenarios, which can occur in function modules. The function module checks for any type of error and raise exception and returns SY-SUBRC to the calling program. Main program checks this SY-SUBRC for any errors that have occurred and then takes action accordingly.

To call a function module from an ABAP/4 program, use the CALL statement as follows:

Syntax:

CALL FUNCTION <module>

[EXPORTING

f1 = s1

f2 = s2

fn = sn (parameters which you pass from program to function are

s1, s2, sn)]

[IMPORTING

f1 = r1

f2 = r2

fn = rn (parameters which program receives you pass from function in

r1, r2, rn)]

[TABLES f1 = a1 … fn = an]

EXCEPTIONS notvalid = 1

not correct = 2

OTHERS = 5].

You can specify the name of the function module <module> as a literal or as a variable. Parameters are passed to and from the function module by exactly assigning the actual parameters to the formal parameters in the lists after the EXPORTING, IMPORTING, TABLES.

If in your function if you have raised exception not valid then this exception can be handled in main program. Functions return different sy-subrc for different exceptions.

Ex:

To create a physical file name from a logical file name in your ABAP programs, use the function module FILE_GET_NAME. To insert the function module call in your program, choose Edit Insert statement from the ABAP Editor screen. A dialog box appears. Select Call Function and enter FILE_GET_NAME. The parameters of this function module are listed below.

Import parameters

Parameters----Function

CLIENT -


The maintenance tables for the logical files and paths are clientdependent. Therefore, the desired client can be imported. The current client is stored in the system field SY-MANDT.

LOGICAL_FILENAME---- Enter the logical file name in upper case letters that you want to convert.

OPERATING_SYSTEM----You can import any operating system that is contained in the list in Transaction SF04. The physical file name will be created according to the syntax group to which the operating system is linked. The default parameter is the value of the system field SY-OPSYS.

PARAMETER_1

PARAMETER_2

--If you specify these import parameters, the reserved words

<PARAM_1> and <PARAM_2> in the physical path names will be replaced by the imported values.

USE_PRESENTATION _SERVER

--With this flag you can decide whether to import the operating

system of the presentation server instead of the operating system imported by the parameter OPERATING_SYSTEM.

WITH_FILE_EXTENSION If you set this flag unequal to SPACE, the file format defined for the logical file name is appended to the physical file name.

Export Parameters

Parameters-------Function

EMERGENCY_FLAG---If this parameter is unequal to SPACE, no physical name is defined in the logical path. An emergency physical name was created from table

FILENAME and profile parameter DIR_GLOBAL.

FILE_FORMAT--This parameter is the file format defined for the logical file name. You can use this parameter, for example, to decide in which mode the file should be opened.

FILE_NAME--This parameter is the physical file name that you can use with the ABAP statements for working with files.

Exception Parameters

Parameters----Function

FILE_NOT_FOUND--This exception is raised if the logical file is not defined.

OTHERS--This exception is raised if other errors occur.

Source Code:

DATA: FLAG,

FORMAT(3),

FNAME(60).

WRITE SY-OPSYS.

CALL FUNCTION 'FILE_GET_NAME'

EXPORTING

LOGICAL_FILENAME = 'MYTEMP'

OPERATING_SYSTEM = SY-OPSYS

PARAMETER_1 = '01'

IMPORTING

EMERGENCY_FLAG = FLAG

FILE_FORMAT = FORMAT

FILE_NAME = FNAME

EXCEPTIONS

FILE_NOT_FOUND = 1

OTHERS = 2.

IF SY-SUBRC = 0.

WRITE: / 'Flag :', FLAG,

/ 'Format :', FORMAT,

/ 'Phys. Name:', FNAME.

ENDIF.

Regards,

Bhaskar