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

Former Member
0 Likes
14,991

hi everybody:

how to create Exceptions in Funciton Module ?

can give any tips ? tks !

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
10,478

Hi,

Exceptions

The exception of a function module are defined on the Exceptions tab page in the Function Builder. Here you can select exception classes to define whether class-based exceptions are declared or non-class-based exception are defined. Class-based exceptions are represented in the above syntax by RAISING, and non-class-based exceptions are represented by EXCEPTIONS.

The addition RAISING is used to declare class-based exceptions that can be propagated from the function module to the caller. Exceptions in the categories CX_STATIC_CHECK and CX_DYNAMIC_CHECK must be explicitly declared, otherwise a propagation can lead to an interface violation. A violation of the interface leads to the treatable exception CX_SY_NO_HANDLER. Exceptions of the category CX_NO_CHECK are implicitly always declared. The declaration of exceptions of the category CX_STATIC_CHECK is statically checked in the syntax check. For exceptions of the category CX_DYNAMIC_CHECK, the check is not performed until runtime. In a function module in which class-based exceptions are declared with the RAISING addition, the statement CATCH SYSTEM-EXCEPTIONS cannot be used. Instead, the relevant treatable exceptions should be handled in a TRY control structure.

The addition EXCEPTIONS is used to define a list of non-class-based exceptions that can be triggered in the function module using the statements RAISE or MESSAGE RAISING. Exceptions defined in this way - as with formal parameters - are bound to the function module and cannot be propagated. If an exception of this type is triggered in a function module, and no return value has been assigned to it with the homonymous addition EXCEPTIONS of the CALL FUNCTION statement when the call was made, this leads to a runtime error.

Note

For new developments after release 6.10, SAP recommends that you work with class-based exceptions that are independent of the function module.

Check this link:

http://help.sap.com/saphelp_erp2004/helpdata/en/9f/db988735c111d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/ef/d94b78ebf811d295b100a0c94260a5/frameset.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/43/41341147041806e10000000a1553f6/frameset.htm

Reward If Helpful.

Regards Madhu.

6 REPLIES 6
Read only

Former Member
0 Likes
10,478

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.

refer to any std fm.

u will get an idea.

Hope this is helps,Do reward.

Read only

0 Likes
10,478

Exercise 1: Creating a Function module

Use

Function modules are ABAP routines that are administered in a central function library. They apply across applications and are available throughout the system. You must assign function modules to a function pool that is called a function group. A function group is nothing but a container for the function modules. Now create a function group and then a function module, which you can use for the next exercise steps.

Procedure

Create a Function Group:

To open the Object Navigator choose from the SAP Menu Overview ® Object Navigator.

From the Object Selection window select Function group, enter FG_Tutorial as name of your function group, and choose Display.

Since the function group FG_Tutorial does not yet exist, the system asks you whether to create it. Confirm with Yes.

Enter a short description and choose Save.

In the window Create Object Catalog Entry choose Local object.

You created a local function group. Before it can receive function modules, you must activate it.

In the object list, use the right mouse button to select the function group FG_Tutorial you just created and choose Activate.

On the next screen choose Continue.

The function group is now active.

Create Global Data:

Within the function group you can declare global data. All function modules of this function group share this global data.

In the Object Selection window open the directory tree Includes and double-click on LFG_TutorialTOP.

Choose Create « Change ( STRG+F1 ) and enter the following data declarations in the tool area:

TABLES spfli.

DATA spfli_workarea LIKE spfli.

Check ( STRGF2 ) and activate ( STRGF3 ) the include file.

Create a Function Module:

Create a function module that reads data from table SPFLI .

If you are not yet in the Object Navigator, choose from the SAP Menu Overview à Object Navigator and display the function group FG_Tutorial .

In the Object selection window, use the right mouse button to select function group FG_Tutorial , and choose Create à Function module.

Enter as function module name XX_RFC_READ_SPFLI and replace XX with the initials of your name.

Enter a short description and choose Save.

The system lists the function module in the object list in a new directory Function modules, and displays it on the right side in the Function Builder.

Select the Attributes tab and under Processing type choose Remote-enabled module.

Select the Import tab and enter in the appropriate columns the names of the import parameters: carrid with reference type like spfli-carrid and connid with reference type like spfli-connid . For each parameter set the Pass value flag.

Select the Export tab and enter in the appropriate columns the names of the export parameters: ex_spfli with reference type like spfli and sys with reference type like sy-sysid . For each parameter set the Pass value flag.

Select the Exceptions tab and enter the exception invalid_data .

Select the Source code tab.

The system copies the entries you made in the other tabs and creates a source text.

Complete the function module with the actual flight data retrieval.

Compare your function with the model solution.

Save the function module ( STRG+S ). When the note on remote-enabled function modules appears, choose Continue.

Check the function for errors ( STRGF2 ) and then activate it ( STRGF3 ).

Test the Function Module:

Choose Test/Execute ( F8 ) or Function module à Test à Test function module.

As import parameter for CARRID enter LH ; for CONNID enter 400 . Leave the RFC target system line empty.

Choose Execute ( F8 ).

The system displays the export parameters EX_SPFLI and SYS in an additional table.

For a detailed display of the data select the value in line EX_SPFLI.

Or

You can only create function modules and function groups using the Function Builder in the ABAP Workbench. For further information, refer to Creating New Function Modules. This section uses an example to illustrate how a function module is created from the point of view of ABAP programming.

Function Groups and Function Modules:

Firstly, we create a new function group DEMO_SPFLI to hold the function module. Then, we can create the new function module.

Parameter Interface:

You can specify the types of interface parameters in function modules in the

same way as the parameter interfaces of subroutines. Since function

modules can be used anywhere in the system, their interfaces can only contain

references to data types that are declared systemwide. These are the elementary

ABAP data types, the systemwide generic types, such as ANY TABLE, and types

defined in the ABAP Dictionary. You cannot use LIKE to refer to data types

declared in the main program.

Exceptions:

Our function module needs an exception that it can trigger if there are no entries

in table SPFLI that meet the selection criterion. The exception NOT_FOUND

serves this function.

Source Code:

Having defined the parameter interface and exceptions, we can now write the

source code of our function module. To do this, choose Source code in the

Function Builder. This opens the ABAP Editor for the include program

L 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, SYMSGTY,

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.

refer this link

http://help.sap.com/saphelp_nw04/helpdata/en/26/64f623fa8911d386e70000e82011b8/content.htm

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

Reward points..

Read only

Former Member
0 Likes
10,478

Hi,

In Tcode SE37>Enter FM name -


>Exception tab> In Each line you can write you exception and short description.

After that In code where ever you want to raise that exception use below syntax

RAISE except.

Here except--> is whatever you have declared exception in FM

Read only

Former Member
0 Likes
10,478

hi Sophie xue,

Creating Function Modules

You can only create function modules and function groups using the Function Builder in the ABAP Workbench. For further information, refer to Creating New Function Modules. This section uses an example to illustrate how a function module is created from the point of view of ABAP programming.

We are going to create a function module READ_SPFLI_INTO_TABLE to read data for a specified airline from table SPFLI into an internal table, which it then passes back to the calling program.

Function Groups and Function Modules

Firstly, we create a new function group DEMO_SPFLI to hold the function module (see Creating a Function Group). Then, we can create the new function module (see Creating a Function Module).

Parameter Interface

You can specify the types of interface parameters in function modules in the same way as the parameter interfaces of subroutines. Since function modules can be used anywhere in the system, their interfaces can only contain references to data types that are declared systemwide. These are the elementary ABAP data types, the systemwide generic types, such as ANY TABLE, and types defined in the ABAP Dictionary. You cannot use LIKE to refer to data types declared in the main program.

The function module READ_SPFLI_INTO_TABLE requires an import parameter to restrict the selection to a single airline. To specify the type, we can refer to the key field CARRID of the database SPFLI:

Under Ref. field/structure, you can enter a column of a database table, a component of a ABAP Dictionary structure, or a whole ABAP Dictionary structure. The import parameter ID is optional, and has a default value.

The following type specification would have the same effect:

Ref. type can contain any generic or full data type that is recognized systemwide. Here, the parameter is defined with reference to the elementary ABAP Dictionary type (or data element) S_CARR_ID. This is the type used to define the field SPFLI-CARRID.

To pass data back to the calling program, the function module needs an export parameter with the type of an internal table. For this, we define a systemwide table type SPFLI_TAB with the line type SPFLI in the ABAP Dictionary.

We can now use this data type to specify the type of the export parameter ITAB:

The internal table is passed by value. Only internal tables that are passed using tables parameters can be passed exclusively by reference.

Exceptions

Our function module needs an exception that it can trigger if there are no entries in table SPFLI that meet the selection criterion. The exception NOT_FOUND serves this function:

Source Code

Having defined the parameter interface and exceptions, we can now write the source code of our function module. To do this, choose Source code in the Function Builder. This opens the ABAP Editor for the include program L 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.

Calling READ_SPFLI_INTO_TABLE

The following program calls the function module READ_SPFLI_INTO_TABLE:

REPORT DEMO_FUNCTION_MODULE.

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.

If Found Helpfull.

Do Reward.

Regards.

Eshwar.

Read only

Former Member
0 Likes
10,479

Hi,

Exceptions

The exception of a function module are defined on the Exceptions tab page in the Function Builder. Here you can select exception classes to define whether class-based exceptions are declared or non-class-based exception are defined. Class-based exceptions are represented in the above syntax by RAISING, and non-class-based exceptions are represented by EXCEPTIONS.

The addition RAISING is used to declare class-based exceptions that can be propagated from the function module to the caller. Exceptions in the categories CX_STATIC_CHECK and CX_DYNAMIC_CHECK must be explicitly declared, otherwise a propagation can lead to an interface violation. A violation of the interface leads to the treatable exception CX_SY_NO_HANDLER. Exceptions of the category CX_NO_CHECK are implicitly always declared. The declaration of exceptions of the category CX_STATIC_CHECK is statically checked in the syntax check. For exceptions of the category CX_DYNAMIC_CHECK, the check is not performed until runtime. In a function module in which class-based exceptions are declared with the RAISING addition, the statement CATCH SYSTEM-EXCEPTIONS cannot be used. Instead, the relevant treatable exceptions should be handled in a TRY control structure.

The addition EXCEPTIONS is used to define a list of non-class-based exceptions that can be triggered in the function module using the statements RAISE or MESSAGE RAISING. Exceptions defined in this way - as with formal parameters - are bound to the function module and cannot be propagated. If an exception of this type is triggered in a function module, and no return value has been assigned to it with the homonymous addition EXCEPTIONS of the CALL FUNCTION statement when the call was made, this leads to a runtime error.

Note

For new developments after release 6.10, SAP recommends that you work with class-based exceptions that are independent of the function module.

Check this link:

http://help.sap.com/saphelp_erp2004/helpdata/en/9f/db988735c111d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/ef/d94b78ebf811d295b100a0c94260a5/frameset.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/43/41341147041806e10000000a1553f6/frameset.htm

Reward If Helpful.

Regards Madhu.

Read only

0 Likes
10,478

has solved it ,rewarded all ,tks .