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: 

Object oriented exception concept -

Former Member
0 Kudos

Hi all,

I'm trying to get familiar with the object oriented exception concept, but there's one point which I did not understand so far:

I created an exception class inheriting from class 'CX_DYNAMIC_CHECK'. In order to store additional information, I added some additional attributes to my class that should be filled when the exception is raised.

So I tried to add additional import parameters to the constructor and to fill the additional attributes within the constructor, but I'm not able to change the constructor at all, i.e. I can't add additional import parameters nor am I able to change the source code of the constructor.

Can you give me any hint how to deal with additional attributes?

Best regards,

Markus

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

In order to pass more values / parameters in the CONSTRUCTOR, you need to create them as the Attributes of the class. As soon as you create it as attribute, it would be available as the signature of the CONSTRUCTOR. E.g if you add V_MESSAGE as the attribute, system would create a importing signature V_MESSAGE of CONSTRUCTOR and also add the code to populate the attribute V_MESSAGE from importing parameter V_MESSAGE.


method CONSTRUCTOR.
CALL METHOD SUPER->CONSTRUCTOR
EXPORTING
TEXTID = TEXTID
PREVIOUS = PREVIOUS
.
me->IV_MESSAGE = IV_MESSAGE .  " <<
endmethod.

You can use this to pass the value while Raising the Exception:


RAISE EXCEPTION TYPE ZCX_TEST_NP  EXPORTING
  V_MESSAGE = 'This is test'.

When you catch it, you can actually use the attribute to access the value:


  data: o_Exc type ref to zcx_test_np.
  catch zcx_test_np into o_exc.
     write: / 'Exception', o_Exc->v_message.

Regards,

Naimesh Patel

2 REPLIES 2

naimesh_patel
Active Contributor
0 Kudos

In order to pass more values / parameters in the CONSTRUCTOR, you need to create them as the Attributes of the class. As soon as you create it as attribute, it would be available as the signature of the CONSTRUCTOR. E.g if you add V_MESSAGE as the attribute, system would create a importing signature V_MESSAGE of CONSTRUCTOR and also add the code to populate the attribute V_MESSAGE from importing parameter V_MESSAGE.


method CONSTRUCTOR.
CALL METHOD SUPER->CONSTRUCTOR
EXPORTING
TEXTID = TEXTID
PREVIOUS = PREVIOUS
.
me->IV_MESSAGE = IV_MESSAGE .  " <<
endmethod.

You can use this to pass the value while Raising the Exception:


RAISE EXCEPTION TYPE ZCX_TEST_NP  EXPORTING
  V_MESSAGE = 'This is test'.

When you catch it, you can actually use the attribute to access the value:


  data: o_Exc type ref to zcx_test_np.
  catch zcx_test_np into o_exc.
     write: / 'Exception', o_Exc->v_message.

Regards,

Naimesh Patel

Former Member
0 Kudos

Thanks a lot!