Application Development 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: 

Function Module Exceptions handling

Former Member
0 Kudos

Hi all,

I dont know how to handle exceptions. For example I have given code below. In that do I need to put any extra logic or not.

CALL FUNCTION 'BDC_CLOSE_GROUP'

EXCEPTIONS

NOT_OPEN = 1

QUEUE_ERROR = 2

OTHERS = 3

.

IF SY-SUBRC <> 0.

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

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Thanks,

Shashikanth.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

You can do something like this -

<b>If SY-SUBRC NE 0.

Write:/ 'Error in Function BDC_CLOSE_GROUP',

/ 'SY-SUBRC', SY-SUBRC .

ENDIF.</b>

This will help you analyze where and what error occured and after which function call.

Cheers.

6 REPLIES 6

Former Member
0 Kudos

This piece of code is exception handling -

IF SY-SUBRC <> 0.

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

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

SY-SUBRC will be set to the number which is defined against the exception in function call. In your example if execption NOT_OPEN occurs the sy-subrc will be set to 1. You should code between if endif suitable message so that the user running the program knows what error has occured .

Not having sy-subrc check after function call means you are not handling exception and a dump will occur if an exception is raised.

Cheers.

0 Kudos

Hi Sanjay,

For example if error 1 occurs, what should i add and where should i add . I mean just give me some explanation for th, sothat I will do according to that.

Thank yoy,

shashikanth

Former Member
0 Kudos

I generally replace:


IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

with something like:

IF SY-SUBRC <> 0.
MESSAGE E205 WITH ...
* and here put some logic to decide what to do if
* the FM fails.
ENDIF.

If you put a message of type E, that will stop the program, but if you don't stop the program, further logic may be needed. It depends on your requirements.

Rob

Message was edited by: Rob Burbank

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can handle exceptions for function modules in the following way. Say that you wanted to do something different for each exception.

[code]CALL FUNCTION 'BDC_CLOSE_GROUP'

EXCEPTIONS

NOT_OPEN = 1

QUEUE_ERROR = 2

OTHERS = 3

.

case sy-subrc.

When '1'.

Write:/ 'not open'.

when '2'.

write:/ 'queue error'.

when '3'.

write:/ 'others'.

endcase.

code]

Regards,

Rich Heilman

Former Member
0 Kudos

You can do something like this -

<b>If SY-SUBRC NE 0.

Write:/ 'Error in Function BDC_CLOSE_GROUP',

/ 'SY-SUBRC', SY-SUBRC .

ENDIF.</b>

This will help you analyze where and what error occured and after which function call.

Cheers.

Former Member
0 Kudos

Hi Shashikanth,

Error handling is always specific to your needs. It can vary from just issueing a message to sending a detailed email to someone. It depends on your requirements to define how far you want to go.

But the important this is, you need to uncomment the EXCEPTIONS part of it and the IF SY-SUBRC <> 0 is where you write all your error handling code. You don't necessarily have to use the return codes, unless you want to give detailed messages. Also you don't have to send an error message, you can just change the type to information message. You can collect the error message into an internal table and write them out at the end. Or you may not want to send any messages, but just stop the process.

So there are several possibilities as to how you want to handle the error. The function module is going to tell that an error has occured and this is the error message. It is up to you, as to how you want to react to this condition.

Srinivas