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: 

How to catch an error message that raises an exception?

Former Member
0 Kudos

Hello.

Here is the problem:

Function_1 calls Function_2 without providing exceptions in the call. Function_2 raises an exception (message e(xxx) raising yyy) and because it isnt handled by function_1 a nasty error message is displayed.

Question - how to catch that error message when calling function_1?

Even more - I am not sure wether other errors that arent treated in the code may pop up during the function module execution. It there a proof way to catch all of them?

I was considering TRY/CATCH but Im not sure about the exact usage of this block in this particular case.

Thanks for your help.

1 ACCEPTED SOLUTION

former_member199581
Active Participant
0 Kudos

TRY/CATCH it's not applicable in case of Function Module, to use TRY you need class-based exceptions.

Use only CATCH:


CATCH.
 CALL FUNCTION "Function_1".
   IF sy-subrc NE 0.
    do_something.
   ENDIF.
ENDCATCH.

However, you can add the "EXCEPTIONS" statement to the first CALL FUNCTION, catching a particular value of sy-subrc, like this:

EXCEPTIONS others = 20.

Then, check subrc:


CATCH.
 CALL FUNCTION "Function_1"
   EXCEPTIONS others = 20.
   IF sy-subrc = 20.
     do_something.
   ENDIF.
ENDCATCH.

Hope this helps,

R.

7 REPLIES 7

former_member199581
Active Participant
0 Kudos

TRY/CATCH it's not applicable in case of Function Module, to use TRY you need class-based exceptions.

Use only CATCH:


CATCH.
 CALL FUNCTION "Function_1".
   IF sy-subrc NE 0.
    do_something.
   ENDIF.
ENDCATCH.

However, you can add the "EXCEPTIONS" statement to the first CALL FUNCTION, catching a particular value of sy-subrc, like this:

EXCEPTIONS others = 20.

Then, check subrc:


CATCH.
 CALL FUNCTION "Function_1"
   EXCEPTIONS others = 20.
   IF sy-subrc = 20.
     do_something.
   ENDIF.
ENDCATCH.

Hope this helps,

R.

Former Member
0 Kudos

hi

good

you cant use the try/catch i would suggest you to better use the statement like

if SY-SUBRC EQ 0.

MESSGE MESSID NO.

ENDIF.

In this way you can display your own message if there is any problem while function_1 calls the function_2.

thanks

mrutyun^

Former Member
0 Kudos

Hmm...

I see a problem with EXCEPTIONS.

First - Function_1 doesn't have any exceptions in the interface even though function_2 does.

Secondly - as soon as function_2 issues error message everything is stopped and I am not getting back to initial program to process the sy-subcr.

Or am i missing something?

Former Member
0 Kudos

Welcome to SDN.

You won't be able to handle the exception raised by Function_2 unless you can change Function_1. Function_1 needs to map all of the potential exceptions raised by Function_2 to return codes and map them to its own exceptions, which you can then map and handle when you call Function_1.

The exception can only be handled by the direct calling code, in this case Function_1.

Regards,

Nick

0 Kudos

thanks. that what i was afraid of

Former Member
0 Kudos

Hmmm

Looks like I have the answer.

Just call function_1 with "error_message" EXCEPTION even if FM itself doesn't have any exceptions in the interface.

CALL FUNCTION 'FUNCTION_1'

EXCEPTIONS

error_message = 01.

function_1 will return subrc=1 and relevant message data if function_2 raises any exception

Message was edited by:

Jurijs Tolokoncevs

0 Kudos

Well done, it's even there in the ABAP help if you look really hard.

Proves (as if proof were needed) that I don't know as much as I think I do

Regards,

Nick