‎2021 Feb 04 2:35 PM
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
‎2021 Feb 04 2:55 PM
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
‎2021 Feb 04 3:00 PM
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) ).
‎2021 Feb 04 3:25 PM
frdric.girod Do you say that it is better to have many exception classes , which are united in packages?
‎2021 Feb 04 5:13 PM
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?
‎2021 Feb 04 5:25 PM
‎2021 Feb 04 5:34 PM
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?
‎2021 Feb 04 6:05 PM
Yes (it's how I do in those cases, I like KISS and YAGNI principles)
But probably people will have other opinions.
‎2021 Feb 04 8:03 PM
‎2021 Feb 04 8:58 PM
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?
‎2021 Feb 05 7:39 AM
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.)
‎2021 Feb 05 8:12 PM
‎2021 Feb 06 8:58 AM
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.
‎2021 Feb 06 9:55 AM
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.
‎2021 Feb 07 7:03 AM
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
‎2021 Feb 07 7:52 AM
sandra.rossi , Thanks for your anaswer.
Do you create an exception for warning? I thought that an exception is for blocking.
‎2021 Feb 07 8:31 AM
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) ).