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

Catch System Exception

Former Member
0 Likes
4,772

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,854

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

4 REPLIES 4
Read only

Former Member
0 Likes
1,854

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

Read only

Former Member
0 Likes
1,854

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

Read only

Former Member
0 Likes
1,854

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

Read only

Former Member
0 Likes
1,855

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