‎2006 Nov 28 11:58 AM
Hello Guyz!
need info about handling system exception while making type conversion. for example, i have 3 variables, viz.
<b>v1 type p,
v2 type f,
v3 type i.</b>
now i am going to divide v2 by v3 and store result in v1, i.e.
<b>v1 = v2 / v3.</b>
all i need is, in this particular case, if system ever gives any exception and if so,then is there any existing system defined exception to handle this.
shane
‎2006 Nov 28 12:19 PM
Shane,
Check the sample code to handle the exceptions.
REPORT ZTEST.
PARAMETERS number TYPE i.
DATA: result TYPE p 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
Vijay
‎2006 Nov 28 11:59 AM
HI
Please check this thread it will be of use to you
https://forums.sdn.sap.com/click.jspa?searchID=203614&messageID=435879
This is also a good one
https://forums.sdn.sap.com/click.jspa?searchID=203614&messageID=884156
Message was edited by:
Dominic Pappaly
‎2006 Nov 28 12:02 PM
HI,
Catchable runtime errors are handled with CATCH SYSTEM-EXCEPTIONSusing the name of the runtime error. To detect semantically related runtime errors using a common name, they are combined into exception groups.
You can handle catchable runtime errors in an ABAP program using the following control statements:
CATCH SYSTEM-EXCEPTIONS exc1 = rc1 ... excn = rcn.
...
ENDCATCH.
<u>Smaple program:-</u>
REPORT demo_catch_endcatch.
DATA: result TYPE p DECIMALS 3,
number TYPE i VALUE 11.
CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 5.
DO.
number = number - 1.
result = 1 / number.
WRITE: / number, result.
ENDDO.
ENDCATCH.
SKIP.
IF sy-subrc = 5.
WRITE / 'Division by zero!'.
ENDIF
Regards
Sudheer
‎2006 Nov 28 12:03 PM
Hi,
Look at this SAP help link .... This is having a Example Program also
http://help.sap.com/saphelp_nw2004s/helpdata/en/a9/b8eef8fe9411d4b2ee0050dadfb92b/content.htm
‎2006 Nov 28 12:19 PM
Shane,
Check the sample code to handle the exceptions.
REPORT ZTEST.
PARAMETERS number TYPE i.
DATA: result TYPE p 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
Vijay