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

CX_ROOT

Former Member
0 Likes
7,535

Hi All,

I have just started learning SAP ABAP Object Programming Concepts and believe me i have read quite a few documents before posting this question about Exception Handling

In both Examples i get the Message Division by zero because CX_ROOT is Super class of CX_SY_ZERODIVIDE ...


My Question

1)why we always write Catch CX_ROOT into gr_err why be specific with Catch CX_SY_ZERODIVIDE ...

I'm thing always writing Catch CX_ROOT into gr_err for any Statements in TRY and END TRY

TRY.

-----

-----

-----

CATECH CX_ROOT into gr_err.

     gs_msg = gr_err->get_text( )..

ENDTRY.

  write : gs_msg.


Example 1)

DATA :

   a TYPE i ,

   b TYPE i VALUE 10.

TRY.

     a = b / 0.

  CATCH cx_sy_zerodivide INTO gr_err.

       gs_msg = gr_err->get_text( )..

ENDTRY.

write : gs_msg.

Example 2)

DATA :

   a TYPE i ,

   b TYPE i VALUE 10.

TRY.

     a = b / 0.

CATCH CX_ROOT INTO gr_err.

       gs_msg = gr_err->get_text( )..

ENDTRY.

write : gs_msg.

Please any clear explaining would be help no links please

Thanks in Advance

5 REPLIES 5
Read only

Former Member
0 Likes
4,102

Hi Raju,

cx_root is the root element of exception classes.

Every other class is inherited by it.

If you use the Exception is defined as any Exception class, you can catch it specified as cy_sy_zerodivide or as cx_root.

If you do the first type, you can handle each exception different.

i hope it helps,

Bernhard

Read only

0 Likes
4,102

Thanks Bernhard...

Can you be more clear ...With an example..Im trying to get my basics right

if you use the Exception is defined as any Exception class, you can catch it specified as cy_sy_zerodivide or as cx_root.

If you do the first type, you can handle each exception different.

Thanks n Advance

Read only

0 Likes
4,102

Sometimes it is needed to catch like this:

DATA :

   a TYPE i ,

   b TYPE i VALUE 10.

TRY.

     a = b / 0.

  CATCH cx_sy_zerodivide INTO gr_err.

       gs_msg = gr_err->get_text( )..

  CATCH cx_sy_systemerror INTO gr_err.

     " We don't want to do something if this error appears.

ENDTRY.

write : gs_msg.

when using cx_root all Messages handled the same way -> get_text writes the string into gs_msg

TRY.

     a = b / 0.

CATCH CX_ROOT INTO gr_err.

       gs_msg = gr_err->get_text( )..

ENDTRY.

Read only

alejandro_mejias
Active Participant
0 Likes
4,102

You can use CX_ROOT to catch any derived exception, such us CX_SY_ZERODIVIDE. But you should use CX_SY_ZERODIVIDE and many other derived classes in order to do a different treatment in each exception. It depends on your needs . For instance, if you do arithmetic operations, probably you will have different exception possilities (zero divid, arithmetic overflow).... You can simply display an error message (in this case CX_ROOT catch will be enough) or you can do a more sophisticated error treatment, for instance, fill some field with '#DIV BY 0#' or '#OVERLFOW#'....

Hope this will help solve your question.

Also, remember a subclass CAN be defined as RESUMABLE, although CX_ROOT isn't. So you can use the RESUME statement with some derived exceptions, but not with CX_ROOT.

Read only

matt
Active Contributor
0 Likes
4,102

Discussion continued here: http://scn.sap.com/message/14452514

This thread assumed answered and locked.