3

Since .NET 1.1 or 2.0 the Microsoft had changed recommendations for definition of user exceptions. It is now recommended to inherit a user exceptions from System.Exception instead of System.ApplicationException. I found this question on StackOverflow, but it just repeats MSDN recommendation in a form of FxCop rule. However, the explanation given in the FxCop rule is also not convincing. From what you inherit user exceptions in your application and why?

Community
  • 1
  • 1
Leo Y
  • 659
  • 7
  • 22
  • In 99% of cases i innerhit from System.Exception. If i need something more special like a special ArgumentException i would innerhit from System.ArgumentException. But normally a ArgumentException is enough and I don't need custom exceptions for this. Exception is just the first root in the Exception-Tree. – Sebi Nov 16 '16 at 06:45
  • I see a need in user exception when I am writing a module (application, service, framework) which makes use of exceptions and throws its own which should be distinguished from IO and other .NET framework exceptions. – Leo Y Nov 16 '16 at 06:59
  • There's no such need, if you want it to be distinguished then inherit from System.Exception is more than enough. Which is why the recommendation is exist it already more than enough to inherit from System.Exception inherit from System.ApplicationException is just used if you want to ruin convention – kirie Nov 16 '16 at 07:09

2 Answers2

0

I guess that most programmers inherit form System.Exception as this would be a good practice, for most of the exceptions (stated here too), and most of the programmers follow the best practices. Also I guess this question is primary opinion based, because one may think that it is not necesarry to inherit from System.Exception in some context and it is good to chose some other base, exception class, so this could be a debated approach.

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • 1
    I would say that inheriting from System.ApplicationException states that this exception "belongs" to application and not some specific case like argument or IO. Inheriting from the base exception class looks strange. – Leo Y Nov 16 '16 at 06:58
0

Because inheriting from ApplicationException didn't provide sufficient value to merit the practice. From this MSDN page:

If you are designing an application that needs to create its own exceptions, you are advised to derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • It depends what you define as a "value". I would say that from a designer point of view the fact that I inherit from base Exception class means that my exception class provides some basic info. And if it inherits from ApplicationException then it is just some application level exception. – Leo Y Nov 17 '16 at 15:44