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: 

Invalid operator

Former Member
0 Kudos
293

Hi,

In my program

REPORT ZAC_TAB25.

PARAMETERS:

P_NUM1 TYPE I,

P_NUM2 TYPE I,

P_OPER TYPE C.

DATA:

W_RESULT TYPE I.

CALL FUNCTION 'ZAC_CALCULATE'

EXPORTING

IM_NUM1 = P_NUM1

IM_NUM2 = P_NUM2

IM_OPER = P_OPER

IMPORTING

EX_RESULT = W_RESULT

EXCEPTIONS

INVALID_OPERATOR1 = 1

ZERO_DIVIDE1 = 2

OTHERS = 3

.

IF SY-SUBRC EQ 0.

WRITE:/ 'RESULT IS', W_RESULT.

ELSEIF SY-SUBRC EQ 1.

WRITE:/ 'INVALID OPERATION'.

ELSEIF SY-SUBRC EQ 2.

WRITE:/ 'CANNOT DIVIDE BY ZERO'.

ELSE.

WRITE:/ 'UNKNOWN EXCEPTION'.

ENDIF.

when iam executing the program(F8)with invalid operator like T(p_oper=t), It is givimg result as zero, instead of givimg message as Invalid operator.

Thanks

1 ACCEPTED SOLUTION

Former Member
0 Kudos
123

try like this

CASE IM_OPER.

WHEN '+'.

EX_RESULT = IM_NUM1 + IM_NUM2.

WHEN '-'.

EX_RESULT = IM_NUM1 - IM_NUM2.

WHEN '*'.

EX_RESULT = IM_NUM1 * IM_NUM2.

WHEN '/'.

IF IM_NUM2 = 0.

raise ZERO_DIVIDE1 .

ENDIF.

EX_RESULT = IM_NUM1 / IM_NUM2.

WHEN '%'.

EX_RESULT = IM_NUM1 MOD IM_NUM2.

WHEN OTHERS.

raise INVALID_OPERATOR1 .

ENDCASE.

ENDFUNCTION.

first check zero division in division operator.

regards

shiba dutta

7 REPLIES 7

Former Member
0 Kudos
123

hi Ram,

did u code it in this fashion in your ZAC_CALCULATE' FM ???


if im_oper = 'T'.
  raise INVALID_OPERATOR1.
endif.

0 Kudos
123

Hi Santosh,

This is my function module.

CASE IM_OPER.

WHEN '+'.

EX_RESULT = IM_NUM1 + IM_NUM2.

WHEN '-'.

EX_RESULT = IM_NUM1 - IM_NUM2.

WHEN '*'.

EX_RESULT = IM_NUM1 * IM_NUM2.

WHEN '/'.

EX_RESULT = IM_NUM1 / IM_NUM2.

WHEN '%'.

EX_RESULT = IM_NUM1 MOD IM_NUM2.

WHEN OTHERS.

ENDCASE.

IF IM_NUM2 = 0.

WRITE:/ 'CANNOT DIVIDE BY ZERO'.

ENDIF.

ENDFUNCTION.

Plz let me know if anything i have to write in my function module.

I had maintained exceptions INVALID OPERATOR1 AND ZERODIVIDE.

Former Member
0 Kudos
123

Hi,

What is the value you are sending to FM of W_RESULT?

Can you copy your FM code here?

Former Member
0 Kudos
123

Hi ram,

Can you paste the code of your FM.

Regards,

Atish

0 Kudos
123

Hi Ram,

Code your exception as follos.

CASE IM_OPER.

WHEN '+'.

EX_RESULT = IM_NUM1 + IM_NUM2.

WHEN '-'.

EX_RESULT = IM_NUM1 - IM_NUM2.

WHEN '*'.

EX_RESULT = IM_NUM1 * IM_NUM2.

WHEN '/'.

EX_RESULT = IM_NUM1 / IM_NUM2.

WHEN '%'.

EX_RESULT = IM_NUM1 MOD IM_NUM2.

WHEN OTHERS.

<b>RAISE INVALID_OPERATOR.</b> *See the change

ENDCASE.

Add the same in the EXCEPTIONS of the FUNCTION CALL.

as follows

REPORT ZAC_TAB25.

PARAMETERS:

P_NUM1 TYPE I,

P_NUM2 TYPE I,

P_OPER TYPE C.

DATA:

W_RESULT TYPE I.

CALL FUNCTION 'ZAC_CALCULATE'

EXPORTING

IM_NUM1 = P_NUM1

IM_NUM2 = P_NUM2

IM_OPER = P_OPER

IMPORTING

EX_RESULT = W_RESULT

EXCEPTIONS

<b>INVALID_OPERATOR = 1</b> See the change

ZERO_DIVIDE1 = 2

OTHERS = 3

.

IF SY-SUBRC EQ 0.

WRITE:/ 'RESULT IS', W_RESULT.

ELSEIF SY-SUBRC EQ 1.

WRITE:/ 'INVALID OPERATION'.

ELSEIF SY-SUBRC EQ 2.

WRITE:/ 'CANNOT DIVIDE BY ZERO'.

ELSE.

WRITE:/ 'UNKNOWN EXCEPTION'.

ENDIF.

Regards,

Sesh

Former Member
0 Kudos
123

hi Ram,

do this way

CASE IM_OPER.

WHEN '+'.

EX_RESULT = IM_NUM1 + IM_NUM2.

WHEN '-'.

EX_RESULT = IM_NUM1 - IM_NUM2.

WHEN '*'.

EX_RESULT = IM_NUM1 * IM_NUM2.

WHEN '/'.

EX_RESULT = IM_NUM1 / IM_NUM2.

WHEN '%'.

EX_RESULT = IM_NUM1 MOD IM_NUM2.

WHEN OTHERS.

<b> raise INVALID OPERATION'.</b>

ENDCASE

Former Member
0 Kudos
124

try like this

CASE IM_OPER.

WHEN '+'.

EX_RESULT = IM_NUM1 + IM_NUM2.

WHEN '-'.

EX_RESULT = IM_NUM1 - IM_NUM2.

WHEN '*'.

EX_RESULT = IM_NUM1 * IM_NUM2.

WHEN '/'.

IF IM_NUM2 = 0.

raise ZERO_DIVIDE1 .

ENDIF.

EX_RESULT = IM_NUM1 / IM_NUM2.

WHEN '%'.

EX_RESULT = IM_NUM1 MOD IM_NUM2.

WHEN OTHERS.

raise INVALID_OPERATOR1 .

ENDCASE.

ENDFUNCTION.

first check zero division in division operator.

regards

shiba dutta