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: 

Regarding calling message class using Raise Exception

Former Member
0 Kudos

Hi All,

I have created a ZCX exception class with a Z message class.

When I use this exception class in any other class using ' RAISE EXCEPTION TYPE zcx' , I want directly call the message number from the message class which I have created.

Instead of passing custom message using

RAISE EXCEPTION TYPE zcx

Exporting

message = ' '.

I want to directly call the message class , message number and pass the parameters.

Thanks and Regards.

Rajender patyal

10 REPLIES 10

adam_krawczyk1
Contributor

Hi Rajender,

The power of exception classes is that you can program any methods that you want inside them. So you can keep attributes that you want - text, messages ids, even implement logic do do some calculations.

Let me show you the simplest example that matches requirements.

CLASS lcx_exception DEFINITION INHERITING FROM cx_static_check.

  PUBLIC SECTION.

    METHODS constructor
      IMPORTING i_msg_no TYPE symsgno.

    METHODS display_as_error.

  PRIVATE SECTION.
    DATA m_msg_no TYPE symsgno.

ENDCLASS.                    "lcx_exception DEFINITION

CLASS lcx_exception IMPLEMENTATION.

  METHOD constructor.
    super->constructor( ).
    m_msg_no = i_msg_no.
  ENDMETHOD.                    "constructor

  METHOD display_as_error.

    MESSAGE ID 'ZCL_MESSAGE_CLASS' TYPE 'E' NUMBER m_msg_no.

  ENDMETHOD.                    "raise_error

ENDCLASS.                    "lcx_exception IMPLEMENTATION

START-OF-SELECTION.

  DATA lo_caught_exception TYPE REF TO lcx_exception.
  TRY.
      RAISE EXCEPTION TYPE lcx_exception
        EXPORTING
          i_msg_no = '002'.
    CATCH lcx_exception INTO lo_caught_exception.
      lo_caught_exception->display_as_error( ).
  ENDTRY.

Some explanations:

- I created constructor that accepts message number only.

- lcx_exception class knows that messages must be raised from ZCL_MESSAGE_CLASS.

- There is method display_as_error that displays error message based on saved id (attribute of class).

- It is easy to extend behavior, like if you want you can add method lo_exception->display_as_info( ) or lo_exception->display_as_warning( ) and call it when you need.

- RAISING EXCEPTION TYPE lcx_exception must be put inside TRY CATCH section so you can catch exception into lo_caught_exception object and run any operations you want.

This is the simplest case. In SAP help under MESSAGE term there is also example of implementing IF_T100_MESSAGE interface to have standard and flexible error handling. Then you can raise that kind of exception object with statement:

"MESSAGE lo_exception_with_interface TYPE 'E'.

Hope it helps.

Regards

Adam

0 Kudos

Hi,

Thanks for the reply.

I have used If_T100_  interface for raising exception and displaying messages from Table T100.

Is there any limitation in creating exception Ids.

using Go_oref->get_text(), we only get the message text. I also wanted the message number which i am using from the message class.

There is no method in Go_oref where we could get the message number.

Thanks and Regards.

Rajender

0 Kudos

Hello Rajender,

You should be able to pass the message number, message class, and parameters using the parameter TEXTID when you raise the exception when your exception class is implementing interface IF_T100_MESSAGE.

       ls_t100_key-msgid = '00'.

       ls_t100_key-msgno = '443'.

       RAISE EXCEPTION TYPE zcx_msg_t100

         EXPORTING

           textid = ls_t100_key.

You should be able catch and givee the message using the

CATCH zcx_msg_t100 INTO lo_exc.

       MESSAGE lo_exc TYPE 'I'.

You should be able to access the message attributes using the component access interface IF_T100_MESSAGE attributes. Like:

TRY.

       ls_t100_key-msgid = '00'.

       ls_t100_key-msgno = '443'.

       RAISE EXCEPTION TYPE zcx_msg_t100

         EXPORTING

           textid = ls_t100_key.

     CATCH zcx_msg_t100 INTO lo_exc.

       MESSAGE lo_exc TYPE 'I'.

        MESSAGE ID lo_exc->IF_T100_MESSAGE~T100KEY-msgid

                TYPE 'I'

                NUMBER lo_exc->IF_T100_MESSAGE~T100KEY-msgno

                WITH   lo_exc->IF_T100_MESSAGE~T100KEY-ATTR1.

ENDTRY.

Regards,
Naimesh Patel

matt
Active Contributor
0 Kudos

Have you tried something like:

    CATCH lcx_exception INTO lo_caught_exception. 

     MESSAGE lo_caught_exception TYPE 'E'. 

Former Member
0 Kudos

Hi Naimesh,

Thanks for the reply.

When we go on down this road we have to pass the message number class etc etc.

I have created a exception Id for each of the message for the respective message class using Message text button in the texts tab.

Exception ID     TEXT

Zcx_msg1         User &MSGV1& has no values

Zcx_msg2         User exists in &1

Zcx_msg3         Plant &1 is initial

 

In a message class there can be max 900 messages, so if we create 900 different exception ID. Is it Fine? or we shall raise exception as you have shown.

Later in the program we can call these exception Ids as shown below.

     RAISE EXCEPTION TYPE zcx

      EXPORTING
        textid = zcx->Zcx_msg1 

If we go down this road no need to pass the message number / class just pass in the attributes or the paramters of the message.

My concern is with this approach we end up creating mutilple exception ID. So is the correct way to do or not.

Please let me know ur inputs.

Thanks and Regards.

Rajender

naimesh_patel
Active Contributor
0 Kudos

Hello Rajender,

I don't think there is any limitation here to maintain Text IDs. But, you should logically group your Exception Texts in a separate Exception class.

Lets say you define ZCX_MSG1 using the Message Text. Here you can only select the fields which Instance Attributes in the ZCX class.

     RAISE EXCEPTION TYPE zcx

      EXPORTING
        textid = zcx->Zcx_msg1

So in order to use the ZCX_MSG1 properly, you would need to create an attribute in the class. Then you need to select this attribute instead of the palceholder in the message text as ATTR1 to ATTR2.

When you add any instance attribute to Exception Class, by default that attribute would be added to CONSTRUCTOR. Means it is available when you do RAISE EXCEPTION. So, above code would send the message to the caller.

User   has no values

In order to give proper message, you would need to pass the field MSGV1 when you raise an exception.

RAISE EXCEPTION TYPE zcx

      EXPORTING
        textid = zcx->Zcx_msg1

        MSGV1 = 'ZXYZ'.

This would be able to send the correct user name to the caller and you would get the message like

User ZXYZ has no values

Now think of the even 10 message IDs with at least 1 different parameter. That means, you would need to create them as instance attributes. That means, they are now 10 separate Importing parameter in CONSTRUCTOR. This would make it very very difficult for any one else other than the original designer to use the exception class - another reason you must logical group your exception IDs in separate exception classes.

Thanks,
Naimesh Patel

Former Member
0 Kudos

Hi Naimesh,

Once again thanks a lot for your reply.

Yes I am created a exception class with a message class Zabc. When we add the interface If_T100_Key a new "Message" button appears on the "Texts" tab of the class..

Just click on that button  give the message class and the number and the respective message paramerers. The attributes or the exception Id created will be "Msgclass_Msgnumber". it will easy for the other person to know which message is displayed.

While raising exception, raise it with the exception Id and pass the message attributes.

Yes, as you said all these attributes will be the importing parameters to the constructor.Back to my question if we declare exception Id for each message number of class.Will there be any problem?

As you suggested in previous blogs by passing the message number and message class, again in this case user will not know which message is displayed unless he goes to the message class and everywhere we will have to pass msg class number, attributes.

If we go down this road we will have to just call the exception class=>exception_ID and pass the message parameters.

Please let me know if there is any problem while going down this road or any performance issues.

Please do provide your valuable inputs on this.

Thanks and Regards.

Rajender

0 Kudos

Hi Rajender,

You may create as many exception ids as you want (actually there's a technical limit which is about 16000 if I'm correct). I don't think there will be any performance implication, even because you don't need to pass all parameters for all exception ids; you usually pass only the ones linked to the message.

But as Naimish suggested, it's better to split the exception class if this one becomes to big.

Regards,

Custodio

Former Member
0 Kudos

Hi Custodio,

Thanks for the information.

In any message class we can have maximum 900 messsges, so will have maximum 900 attributes.

It means I am fine with the approach.

Thanks.

0 Kudos

Hi Rajender,

It's probably not going to change anything but:

1 - You can actually have 1000 message per message class (from 000 to 999)

2 - You can have texts ids from different message classes in one exception class, which means you are not limited to "only" 1000; rather you can create as many as you want up to the technical limit (about 16000 as I said. If you want to be sure go to menu class->check->generation limits).

Regards,

Custodio