2007 Jul 11 6:14 AM
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
2007 Jul 11 6:25 AM
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
2007 Jul 11 6:17 AM
hi Ram,
did u code it in this fashion in your ZAC_CALCULATE' FM ???
if im_oper = 'T'.
raise INVALID_OPERATOR1.
endif.
2007 Jul 11 6:20 AM
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.
2007 Jul 11 6:18 AM
Hi,
What is the value you are sending to FM of W_RESULT?
Can you copy your FM code here?
2007 Jul 11 6:21 AM
2007 Jul 11 6:22 AM
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
2007 Jul 11 6:25 AM
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
2007 Jul 11 6:25 AM
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