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

How to design exceptions classes?

Martin_Bschen
Participant
1,204

This is ind of an open ended question, but i would love to hear your opinion on that subject.

I often see code in the wild where one exception class is defined (say for a package) and every method just throw this exception for a number of different reasons. The different reasons are separated by different text-ids.

For example, if we would formulate the following function in an object-oriented way ...

FUNCTION GUI_DOWNLOAD.
  IMPORTING
    ...
  EXPORTING
    ...
  EXCEPTIONS
    FILE_WRITE_ERROR
    NO_BATCH
    GUI_REFUSE_FILETRANSFER
    INVALID_TYPE

... we would define an exception class with different textid.

Is this the way you should define exceptions?

I feel it is very bad idea, because the exception says nothing by itself and you can't see by the signature of the method what are the possible exception situations. I would rather have an exception class for all the 4 reason shown above. What do you think?

3 REPLIES 3
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
Read only

raghug
Active Contributor
1,058

I have a half written blog about this very topic, this gives me the impetus to finish up... stay tuned!

Read only

raghug
Active Contributor
1,058

Here you go, I have finally finished the post! The way that I have outlined in the blog, you don't have redeclare every exception; and, it will also handle some exceptions that you are not otherwise aware of. Handling old-style errors via class based exceptions | SAP Blogs