Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 8150

Catch CX_ROOT vs Specific Exception

$
0
0

Hi All

 

I'm learning SAP ABAP Objects basics , Please no links. I would appreciate if some one could explain..

 

I have a class ZCL_TEST with a method  GET_DATA

 

with

IM_MATNRImportingTypeMATNR

EX_MARA   Exporting Type MARA

and en exception  ZCX_MATERIAL

 

 

method get_data.

SELECT SINGLE * INTO EX_MARA FROM MARA WHERE MATNR = IM_MATNR.

IF SY-SUBRC NE 0.

RAISE ZCX_MATERIAL.

endmethod.

 

Again ZCX_MATERIAL is an Exception Class with Super class CX_STATIC_CHECK

Now my Exception Class Text Says

 


ZCX_MATERIAL

 

 

 

 

 

Now with in Method DEFAULT of ZCL_MAIN Class i have something like this

 

METHOD DEFAULT.

 

DATA : LC_TEST TYPE REF TO ZCL_TEST.

DATA : WA_MARA TYPE MARA.

DATA:   gr_err TYPE REF TO ZCX_MATERIAL.

 

CREATE OBJECT LC_TEST.

TRY.

LC_TEST->GET_DATA ( IMPORTING IM_MATNR = 'ABC'

                                       EXPORTING IM_MARA  = WA_MARA ).

 

CATCH ZCX_MATERIAL INTO GR_ERR.   "Orginal One

gs_msg = gr_err->get_text( ).

 

WRITE : GS_MSG. 

ENDMETHOD.

 

Output is  " An Exception Occured "  ( This is the Value of GS_MSG.

 

 

 

Instead i change it to

 

METHOD DEFAULT.

 

DATA : LC_TEST TYPE REF TO ZCL_TEST.

DATA : WA_MARA TYPE MARA.

DATA:

   gr_err TYPE REF TO cx_root.

 

 

CREATE OBJECT LC_TEST.

 

TRY.

 

LC_TEST->GET_DATA ( IMPORTING IM_MATNR = 'ABC'

                                      EXPORTING IM_MARA  = WA_MARA ).

 

CATCH CX_ROOT INTO GR_ERR.  "Changed One

gs_msg = gr_err->get_text( ).

 

WRITE : GS_MSG. 

 

ENDMETHOD.

 

Output is  " An Exception Occured "  ( This is the Value of GS_MSG. )

 

 

My questions are

 

1)Either way i'm getting Same Message " An Exception Occured " , so whats the point in Catching Specific CATCH ZCX_MATERIAL INTO GR_ERR.

excepton class ...we can always use CATCH CX_ROOT INTO GR_ERR.

 

 

2)Lets Say i want a more specific message then i have to attach a message text

Now my Exception Class Text Says


ZCX_MATERIALEnter a valid value

If i run the pgm both ways my Output is "Enter a Valid value "

 

So when i have to use   CATCH ZCX_MATERIAL INTO GR_ERR.  or when i have to use CATCH CX_ROOT INTO GR_ERR

 

Thanks in advance


Viewing all articles
Browse latest Browse all 8150

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>