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: 

Exception class with more than one exception

hagit
Active Participant
0 Kudos

Hello expert,

When creating an exception class , the system inserts an Exception Id with the same name as the name of the class (eg ZCX_TRY) .

I can add an Exception Id (eg CX_NEW). Moreover, the activate is successful.

I want to raise those exception IDs in a method in another class. Therefore, I tried to add them to the exception tab of the method.

1. The exception Id with the same name as the name of the class (eg ZCX_TRY) was add successfully.

2. The addition of second exception id raised an error.

err.png

How can I create one exception class with many exceptions?

It does not make sense to create a new class for every exception.

Thanks

Hagit

1 ACCEPTED SOLUTION

geert-janklaps
Active Contributor

Hi,

What you're creating is a separate text id. You can trigger the same exception with different underlying messages based on the reason why the exception is triggered.

Have a look at the documentation here (in your case the textid will be zcx_try=>cx_new to assign the message of cx_new to the exception triggered):

Exception Texts - ABAP Keyword Documentation (sap.com)

If you really need separate exceptions instead of messages (e.g. if you want to be able to handle the exceptions in a different way in your calling program), you'll need to create separate exception classes.

Best regards,

Geert-Jan Klaps

7 REPLIES 7

hagit
Active Participant
0 Kudos

To be more accurate : The addition of second exception id raised a request to create a new exception class

geert-janklaps
Active Contributor

Hi,

What you're creating is a separate text id. You can trigger the same exception with different underlying messages based on the reason why the exception is triggered.

Have a look at the documentation here (in your case the textid will be zcx_try=>cx_new to assign the message of cx_new to the exception triggered):

Exception Texts - ABAP Keyword Documentation (sap.com)

If you really need separate exceptions instead of messages (e.g. if you want to be able to handle the exceptions in a different way in your calling program), you'll need to create separate exception classes.

Best regards,

Geert-Jan Klaps

0 Kudos

8d8214c7f9734f45be69f95cc0d5aeee

Thank you for your answer.

Your answer helps a lot when I just want a different message.

It is pity that when I want to handle the exceptions in a different way in the calling program, I have to create separate exception classes.

Best regards

Hagit

0 Kudos

According to 8d8214c7f9734f45be69f95cc0d5aeee 's answer- Do you think that it is a good design to:

1.Create an exception class for every kind of handing an exception.

2.The exception class will contain many text ids.

For example: Create exception clas:

1..ZCX_BLOCKING_MSG – When the calling program needs to display a blocking message, following the catching of the exceptions, the exceptions will be included in this class.

1.1.Add text id for case number 1.

1.2.Add text id for case number 2.

1.3.etc.

2.ZCX_INFORMATION_MSG- When the calling program needs to display an information message, following the catching of the exceptions, the exceptions will be included in this class.

2.1.Add text id for case number 1.

2.2.Add text id for case number 2.

2.3.etc. 3.etc.

Thank you in advance

Hagit

Hi,

Design-wise it's hard to determine without knowing the full requirement. Although the use of exception classes for information messages seems a little bit strange. An exception is a blocking issue, while an information message shouldn't be something that's blocking your process.

Personally I use some kind of message table (e.g. using table type BAPIRET2_T) for logging (information, success messages) and exceptions for blocking errors.

Best regards,

Geert-Jan Klaps

0 Kudos

8d8214c7f9734f45be69f95cc0d5aeee Thanks for your answer.

Do you have only one exception class or more than one. If you have more than one, could you please give examples of the need for more than one?

Thanks, in advance

Hagit

0 Kudos

I am closing this question as the original question was answered.

Thank you 8d8214c7f9734f45be69f95cc0d5aeee

I will open a new question regarding the design.

Hagit