Application Development and Automation 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: 
Read only

Design Exception Class

hagit
Active Participant
0 Likes
3,380

Hello experts,

When an exception is caught, I need to display a message and exit the program. Every case has its own message. The exception is raised by methods, therefor I use an exception class.

I understand that I can create one exception class with many Text Ids. Different methods will raise the exception class with a different Text Id. In that way it is possible to create only one exception class in the entire system.

(See https://answers.sap.com/questions/13235520/exception-class-with-more-than-one-exception.html)

My question is – What is the right design?

Is it recommended to create only one exception class in the entire system or is it better to create several exception classes? Maybe an exception class for each message class? Or a different division of exception classes?

Thanks in advance

Hagit

16 REPLIES 16
Read only

FredericGirod
Active Contributor
3,209

The logic should be to have one exception by class or better by Interface (even if the exception is used by the class)

I try at least to have one exception class by logical set (a set for me is a Package and contains one or several class based on the same subject)

here the CleanCode

https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#exceptions

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,209

It depends. If all exceptions are handled the same way by the using program(s) and only the text varies, then it's useless creating many exception classes for nothing. Moreover, defining abstract text IDs may also not be needed if your using program(s) have the same behavior for all texts. Remember that an exception class works the same way as any other class, the only special thing is that it must inherit from CX_[STATIC|DYNAMIC|NO]_CHECK. e.g. you could implement your unique exception class such as you invoke it this way, with a dynamic text (implement the constructor accordingly + redefine GET_TEXT and GET_LONGTEXT):

RAISE EXCEPTION NEW zcx_xxx( text = 'my text'(001) ).
Read only

hagit
Active Participant
0 Likes
3,209

frdric.girod Do you say that it is better to have many exception classes , which are united in packages?

Read only

hagit
Active Participant
0 Likes
3,209

sandra.rossi Just to be sure that I understood you- The preferred way is to define one exception class with dynamic text. Is it right?

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,209

It depends, as I explained.

Read only

hagit
Active Participant
0 Likes
3,209

If all exceptions are handled the same way by the using program(s) and only the text varies it is better to define one exception class with dynamic text. Is it right?

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,209

Yes (it's how I do in those cases, I like KISS and YAGNI principles)

But probably people will have other opinions.

Read only

hagit
Active Participant
0 Likes
3,209

sandra.rossi Thank you for your answer

Read only

hagit
Active Participant
0 Likes
3,209

sandra.rossi When a program catches an exception, it displays a message and exit. Could you please give an example for a case that the calling program handle the exception in another way?

Read only

FredericGirod
Active Contributor
0 Likes
3,209

I understand the KISS principle, but I prefer the SOLID. And SOLID contains the No-dependency rules.

Sometimes CleanCode is not really the simple way 🙂

(If you have few exception class, used in a lot of place, and someone would like to make a change ... the dependencies will be a nightmare.)

Read only

Jelena_Perfiljeva
Active Contributor
3,209

sandra.rossi Suggestion to convert this to an answer. 🙂

Read only

Sandra_Rossi
Active Contributor
3,209

Example where the calling program handle exceptions in different ways:

  TRY.
create_something( ).
CATCH zcx_already_exists.
log_warning( ).
CATCH cx_root. log_critical_error( ). ENDTRY.
Read only

ranjith9596
Discoverer
0 Likes
3,209

Please try the below one.

Data : oref_root TYPE REF TO cx_root, "Object for Global Exception Class

lv_error_message TYPE char150. "Variable to capture Error Message

TRY.

" <CODE_TO BE_EXECTUTED>

CATCH cx_root INTO oref_root.

lv_error_message = oref_root->get_text( ). "Capture Exception Message

ENDTRY.

Hope this will fulfill your requirement. Let know if this helped you.

Regards,

Ranjith M R.

Read only

hagit
Active Participant
0 Likes
3,209

ranjith9596 Thank you for your answer. You show how to catch an exception and display its text.

My first question is: In the entire system, for exceptions which, are handled the same way by the calling programs, should I create only one exception class with many text ids, or is it better to create several exception classes? If it is better to create several exception classes, what is the preferred division of exception classes?

The second question is: When an exception is caught, usually the calling program display a message and exit. Could you please give an example for a different response to an exception?

Thanks

Hagit

Read only

hagit
Active Participant
0 Likes
3,209

sandra.rossi , Thanks for your anaswer.

Do you create an exception for warning? I thought that an exception is for blocking.

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,209

No, an exception is for an exception, like attempting to create something that already exists. A warning must not be an exception.

log_warning is just a method to log the exception like a warning. But it was not my intention to discuss what is executed inside the catch, my intention was to show how two exceptions can be handled differently, which was your question (should I create only one exception class or several exception classes).

You have difficulties to understand that there can't be one answer to "should I create one or several exception classes". It's like the question "what is the best programming language": it depends.

So, let me repeat, differently. If you just need one exception handling like this:

TRY.
    do_something( ).
  CATCH zcx_any_error INTO DATA(the_error).
    WRITE / the_error->get_text( ).
ENDTRY.

then, I repeat: "you could implement your unique exception class such as you invoke it this way, with a dynamic text (implement the constructor accordingly + redefine GET_TEXT and GET_LONGTEXT)":

RAISE EXCEPTION NEW zcx_any_error( text = 'my text'(001) ).