Search code examples
classexceptionlocalabap

How to add texts messages to exceptions defined by local classes?


I want to raise an exception of a local type, and pass it in the constructor a *text.

This *text is what i want to be returned by calling the get_longtext method, later, when i catch the exception.

Of course i can add an attribute to my class, and redefine the method. It seems to me like there should however be an easier way (like there is in the java-like languages, where you just "have" that).

There's the TEXTID parameter in the inherited constructor for the exceptions. That however points to the "texts" defined as class properties... but that's for global classes.

So is there any way for me to make use of the already existing constructor in an elegant way? Or must I simply do it all by hand?


Solution

  • If you don't want to uses global classes (as recommended), you can try to implement the interface IF_T100_MESSAGE in your local class. First maintain the error messages in any message class via SE91, then define text ids in your local exception class of type SCX_T100KEY. Implement the exception class methods to use additional attributes for the placeholders in your message texts (if necessary).

    Check IF_T100_MESSAGE and CL_MESSAGE_HELPER for details. To get example code, create a global exception class and add the interface IF_T100_MESSAGE and check how the methods are implemented.