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

regarding raise staements in functional module

Former Member
0 Likes
881

in a function module if it is not returning any data and if we give a raise stament

suppose...functional module.

if sy-subrc <> 0.

raise ..statement

endif.

if this tihng happens then will it give short dump or some thing else will happen

what happens when a raise statement is triggered

can you please give a brief idea about it.

1 ACCEPTED SOLUTION
Read only

Sougata
Active Contributor
0 Likes
728

No it will not shortdump if you RAISE the exception correctly. The return code (sy-subrc) will be set according how the FM is called by the calling program.

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

case sy-subrc.
   when 1.
    message e000 with 'Division by 0 is not allowed'.
   when 2.
    message e000 with 'Incorrect operator used'.
  endcase.

Hope this helps!

in a function module if it is not returning any data and if we give a raise stament

suppose...functional module.

if sy-subrc <> 0.

raise ..statement

endif.

if this tihng happens then will it give short dump or some thing else will happen

what happens when a raise statement is triggered

can you please give a brief idea about it.

5 REPLIES 5
Read only

Former Member
0 Likes
728

Hi

To understand message hanlding in FMs...refer this link

<a href=" http://help.sap.com/saphelp_nw04/helpdata/en/9f/db96ef35c111d1829f0000e829fbfe/frameset.htm ">http://help.sap.com/saphelp_nw04/helpdata/en/9f/db96ef35c111d1829f0000e829fbfe/frameset.htm</a>

Regards

Raj

Read only

0 Likes
728

Hi,

In any function module if you are raising an exception, you must catch this in the caller program. That means the calling of this function module should be included in the "TRY CATCH" block, where you can catch the exception thrown by this function module. If you don't have it like this it will definetly give a short dump saying that "Uncatched Exception so and so..".

Regards,

Jashua

Read only

0 Likes
728

can you please tell me how to use CATCH stament

have never used it.

waiting for your reply

Read only

0 Likes
728

Hi,

TRY.

CATCH XXXXXX( Exception name)

Here you can process your exception.

ENDTRY.

See below example code

PARAMETERS number TYPE i.

DATA: result TYPE p LENGTH 8 DECIMALS 2,

oref TYPE REF TO cx_root,

text TYPE string.

TRY.

IF ABS( number ) > 100.

RAISE EXCEPTION TYPE cx_demo_abs_too_large.

ENDIF.

PERFORM calculation USING number

CHANGING result

text.

CATCH cx_sy_arithmetic_error INTO oref.

text = oref->get_text( ).

CATCH cx_root INTO oref.

text = oref->get_text( ).

ENDTRY.

IF NOT text IS INITIAL.

WRITE / text.

ENDIF.

WRITE: / 'Final result:', result.

FORM calculation USING p_number LIKE number

CHANGING p_result LIKE result

p_text LIKE text

RAISING cx_sy_arithmetic_error.

DATA l_oref TYPE REF TO cx_root.

TRY.

p_result = 1 / p_number.

WRITE: / 'Result of division:', p_result.

p_result = SQRT( p_number ).

WRITE: / 'Result of square root:', p_result.

CATCH cx_sy_zerodivide INTO l_oref.

p_text = l_oref->get_text( ).

CLEANUP.

CLEAR p_result.

ENDTRY.

ENDFORM.

Regards,

Sesh

Read only

Sougata
Active Contributor
0 Likes
729

No it will not shortdump if you RAISE the exception correctly. The return code (sy-subrc) will be set according how the FM is called by the calling program.

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

case sy-subrc.
   when 1.
    message e000 with 'Division by 0 is not allowed'.
   when 2.
    message e000 with 'Incorrect operator used'.
  endcase.

Hope this helps!