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: 

hadling exceptions in Function modules used in user exits

Former Member
0 Kudos
1,303

Hello all,

Can any one tell me how to handle exceptions of a Function Module used in User Exits. I mean will it cause any short dump if we uncomment the system defined messages of a Standard F.M in case if the function module raises an exception(Sy-subrc <> 0 ).

Or

Are we not supposed to use exception handling in User Exits.I was getting a short dump when I am using a custom F.M and when I removed the system defined messages it is working fine.

Thanks in advance.

Regards

1 ACCEPTED SOLUTION

Sougata
Active Contributor
0 Kudos
277

Hi Ramesh,

Yes off course you can handle the exceptions of a FM in an user-exit. First you have to think what to do in case the FM raises an exception i.e. exit the user-exit or continue processing or give users a warning/error message etc. Once you know what to do when there is an exception returned there shouldn't be any problems like short-dumps. It is highly recommended that you do handle the exceptions of a FM irrespective where it is called from (a program, a method or an user-exit)

The return code (sy-subrc) will be set according how the FM is called by the calling program (user-exit in your case).

E.g. of a FM raising an exception (Using RAISE statement), let's call this ZFM

Import param: i_var1 type i, i_var2 type i, i_oper type c length 1.

Export param: e_res type i.

Exception: divide_by_zero, invalid_operation.

FM Code:

case i_oper.
 when '+'.
   e_res = i_var1 + i_var2.
 when '-'.
   e_res = i_var1 - i_var2.
 when '*'.
   e_res = i_var1 * i_var2.
 when '/'.
   if i_var2 = 0.
    raise divide_by_zero.
   else.
    e_res = i_var1 / i_var2.
   endif.
 when others.
    raise invalid_operation.
 endcase.

Now suppose calling program (user-exit in your case) ZXYZ calls the above FM ZFM:

data: v_res type i.
 
 call function 'ZFM'
    exporting 
        i_var1 = 5
        i_var2 = 0
        i_oper = '/'
     importing
        e_res = v_res
     exceptions
        divide_by_zero   = 1
        invalid_operation = 2.

At this point, after the FM has been executed, the return code will be set to 1 because exception would have been raised in the FM that divide by zero is not allowed. After this the calling program could process further as per the return code exported by the FM:

<b>This is how you should handle the exceptions returned by the FM in your user-exit</b>

case sy-subrc.
   when 1.
    message e000 with 'Division by 0 is not allowed'.
   when 2.
* set the result to 0 and continue processing the exit
    v_res = 0.
  endcase.

Hope this helps!

Don't forget to reward points.

Cheers,

Sougata.

5 REPLIES 5

naimesh_patel
Active Contributor
0 Kudos
277

What need to do is like, you should uncomment all the exceptions and after that check SY-SUBRC. If it is not set to 0 than some exception was raised.

You can provide your custom defined error message.

Like

  call function 'OPEN_FORM'
....
 EXCEPTIONS
   CANCELED                          = 1
   DEVICE                            = 2
   FORM                              = 3
   OPTIONS                           = 4
   UNCLOSED                          = 5
   MAIL_OPTIONS                      = 6
   ARCHIVE_ERROR                     = 7
   INVALID_FAX_NUMBER                = 8
   MORE_PARAMS_NEEDED_IN_BATCH       = 9
   SPOOL_ERROR                       = 10
   CODEPAGE                          = 11
   OTHERS                            = 12
            .
  if sy-subrc <> 0.
    message e398(00) with 'Error while opening the form'.
  endif.

Regards

Naimesh Patel

Sougata
Active Contributor
0 Kudos
278

Hi Ramesh,

Yes off course you can handle the exceptions of a FM in an user-exit. First you have to think what to do in case the FM raises an exception i.e. exit the user-exit or continue processing or give users a warning/error message etc. Once you know what to do when there is an exception returned there shouldn't be any problems like short-dumps. It is highly recommended that you do handle the exceptions of a FM irrespective where it is called from (a program, a method or an user-exit)

The return code (sy-subrc) will be set according how the FM is called by the calling program (user-exit in your case).

E.g. of a FM raising an exception (Using RAISE statement), let's call this ZFM

Import param: i_var1 type i, i_var2 type i, i_oper type c length 1.

Export param: e_res type i.

Exception: divide_by_zero, invalid_operation.

FM Code:

case i_oper.
 when '+'.
   e_res = i_var1 + i_var2.
 when '-'.
   e_res = i_var1 - i_var2.
 when '*'.
   e_res = i_var1 * i_var2.
 when '/'.
   if i_var2 = 0.
    raise divide_by_zero.
   else.
    e_res = i_var1 / i_var2.
   endif.
 when others.
    raise invalid_operation.
 endcase.

Now suppose calling program (user-exit in your case) ZXYZ calls the above FM ZFM:

data: v_res type i.
 
 call function 'ZFM'
    exporting 
        i_var1 = 5
        i_var2 = 0
        i_oper = '/'
     importing
        e_res = v_res
     exceptions
        divide_by_zero   = 1
        invalid_operation = 2.

At this point, after the FM has been executed, the return code will be set to 1 because exception would have been raised in the FM that divide by zero is not allowed. After this the calling program could process further as per the return code exported by the FM:

<b>This is how you should handle the exceptions returned by the FM in your user-exit</b>

case sy-subrc.
   when 1.
    message e000 with 'Division by 0 is not allowed'.
   when 2.
* set the result to 0 and continue processing the exit
    v_res = 0.
  endcase.

Hope this helps!

Don't forget to reward points.

Cheers,

Sougata.

Former Member
0 Kudos
277

Hello Sougata.

Thanks for your concern.need a small clarification here let us suppose we are using a F.M KNVV_SINGLE_READ and I am passing the importing parameters (o_knvv) to a local work area.

So here can I use clear work area if sy-subrc fails.

Will it cause any short dump if I do in this manner.Please let me know how to proceed.

Thanks in advance.

Regards

Ram

Sougata
Active Contributor
0 Kudos
277

No it is not going to short-dump if you handle the exception the way I've described in my previous post - clearing the work area after sy-subrc <> 0 (when exception raised by the FM) should be fine.

<b>I've spent a lot of time writing previous post for you so please don't forget to reward points.</b>

Cheers,

Sougata.

Former Member
0 Kudos
277

Hi Ramesh,

It is absolutely possible to avoid dump during runtime. Uncomment the Exceptions statments and check sy-subrc value.

When sy-subrc = 0, proceed with your code and after else statment use clear statment for clearing thw work area whatever you want.

Regards,

Amit