‎2013 Oct 18 2:59 PM
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
‎2013 Oct 18 3:49 PM
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
‎2013 Oct 18 4:00 PM
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
‎2013 Oct 18 4:05 PM
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.
‎2013 Oct 18 4:03 PM
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.
‎2013 Oct 20 8:43 PM
Discussion continued here: http://scn.sap.com/message/14452514
This thread assumed answered and locked.