vendredi 24 septembre 2021

Is it okay to throw different exceptions for the exact same problem if it occurs in a different context?

I have a simple problem in my project (C# based Windows Service) which I just cannot wrap my head around.

In several classes of my application I need to load an interval (time span), from the application configuration. In the configuration file this timespan is stored as a string, such as "1d8h". Obviously, the application needs to parse this string before it can use the time span. In one case, I am passing this string as an argument to a constructor while initializing several objects which all have their own time span. In the constructor I am then parsing this string and throwing an ArgumentException if the parsing fails for whatever reason.

Somewhere else in my application I need to do the exact same thing, but this time in a static constructor which is not allowed to have any arguments. In this case, I am throwing a System.Configuration.ConfigurationErrorsException because I am not working with any arguments so throwing an ArgumentException would not be correct in this case.

Is it perfectly fine to throw different exceptions in both cases, despite them being caused by the exact same operation? If not, what should I do instead?

Aucun commentaire:

Enregistrer un commentaire