Application Development 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: 

Global Exception Class

Former Member
0 Kudos

Hi,

I have a global exception class which is inherited from already existing exception class.

I would like to use this class to raise different exceptions.

How to use this global exception class to raise exceptions with different descriptions?

can anybody explain or send a material on this?

Thanks in advance...

Sreenivas Reddy

6 REPLIES 6

naimesh_patel
Active Contributor
0 Kudos

In your global exception class create couple of attributes which you can use to provide different descriptions.

For Example:


EXC_TEXT	Instance Attribute	Public	Type	STRING	Exception Text
MES_TYPE	Instance Attribute	Public	Type	CHAR1	Message Type

Once you add this attributes to your exception class, than they would be avaliable as the signature of the CONSTRUCTOR method of exception class.

Now, you can use this exception class raise exception like:


  RAISE EXCEPTION TYPE zcx_general_exc
    EXPORTING
      exc_text = 'Exception occured'
      mes_type = 'E'.

Regards,

Naimesh Patel

0 Kudos

Hi,

Thanks for the reply...

No, The golobal exception class is 'CX_AI_APPLICATION_FAULT'. It is not providing the parameters with

that name. Do you want me create these parameters in my custom defined global exception class?

Do I have to pass these variables to any value in the constructor definition?

Give complete details...

Thanks in advance,

Sreenivas Reddy.

0 Kudos

Its entirely depends on your requriement. You can inherite a Exception class from this CX_AI_APPLICATION_FAULT or you can inherite the exception class from CX_DYNAMIC_CHECK. But there is no direct way where you can pass your description while raising the Exception CX_AI_APPLICATION_FAULT.

As a general rule, you must not enhance any standard class. So, inherite a Z exception class from CX_AI_APPLICATION_FAULT and follow the instructions as per the previous post.

Regards,

Naimesh Patel

0 Kudos

HI Naimesh,

I have created a custom exception class with the attributes you have given above and these attributes

are available as a import parameters to the constructor class.

We cab raise an exception with some description 'Exception occured' as you have shown above.

Now, my requirement to catch the same exception in later part of the code and I want to get the same

description in the later part of the code. I have written the code as shown below. But, I am not able to

get the same description.

 

data: n1 type i value 0, 
      n2 type i value 5, 
      v_exc_text type string. 

data: v_ex_oref TYPE REF TO ZCX_SAMPLE_EXCEPTION. 

try. 
if n1 = 0. 
  RAISE EXCEPTION TYPE ZCX_SAMPLE_EXCEPTION 
    EXPORTING 
      exc_text = 'Exception occured' 
      mes_type = 'E'. 
else. 
  n2 = n2 / n1. 
endif. 

CATCH ZCX_SAMPLE_EXCEPTION into v_ex_oref. 

v_exc_text = v_ex_oref->get_text( ). 

IF v_exc_text is not initial. 
   MESSAGE e000(zfi) with v_exc_text space space space. 
ENDIF. 

endtry. 

Do I need to modify the code of get_text( ) method? Please suggest, how to handle this?

Thanks,

Sreenivas Reddy.

0 Kudos

I guess you have little misunderstanding of the Exception concept.

You have to RAISE the exception from any processing block (Subroutine, Method etc.) and CATCH the exception in where you call the subroutine.

Check this example. In this example, I am raising the exception from the method DEVIDE of the class LCL_TEST. Now, I call this method DEVIDE in the START-OF-SELECTION block. So, here I need to catch this raised exception by the DEVIDE method. In my example I have used the exception class ZCX_GEN_EXC, you may have to replace it with your exception class ZCX_SAMPLE_EXCEPTION.


CLASS lcl_test DEFINITION.

  PUBLIC SECTION.
    METHODS devide
      IMPORTING
        n1 TYPE i
      CHANGING
        n2 TYPE i
      RAISING
        zcx_gen_exc.

ENDCLASS.                    "lcl_test DEFINITION

START-OF-SELECTION.

  DATA: lo_test TYPE REF TO lcl_test,
        lo_exc  TYPE REF TO zcx_gen_exc,
        lf_n1   TYPE i,
        lf_n2   TYPE i.

  CREATE OBJECT lo_test.

  lf_n1 = 0.
  lf_n2 = 10.

  TRY.
      CALL METHOD lo_test->devide
        EXPORTING
          n1 = lf_n1
        CHANGING
          n2 = lf_n2.
* catching the exception
    CATCH zcx_gen_exc INTO lo_exc.
      MESSAGE lo_exc->wf_etext TYPE 'I'.
  ENDTRY.

*----------------------------------------------------------------------*
CLASS lcl_test IMPLEMENTATION.

  METHOD devide.

    IF n1 = 0.
      RAISE EXCEPTION TYPE zcx_gen_exc
        EXPORTING
          wf_etext = 'Exception occured'
          wf_mtype = 'E'.
    ELSE.
      n2 = n2 / n1.
    ENDIF.

  ENDMETHOD.                    "devide

ENDCLASS.                    "lcl_Test IMPLEMENTATION

Regards,

Naimesh Patel

Former Member
0 Kudos

Answered