Reputation: 1614
It seems that Microsoft Guidelines for creating custom exceptions state that there should be at least 3 public constructors: empty constructor, the one that takes a message and the one that takes a message and inner exception. My question is why do we need public constructors at all? If I m designing a framework that throws some custom exceptions (e.g. SomethingNotFoundException, etc.), then I would expect the framework users to catch my exceptions and handle them, but not throw them, since the APIs in the framework itself would throw them. So why not just have constructors as internal and only expose public properties that users need to handle these exceptions?
Upvotes: 1
Views: 423
Reputation: 1340
Public constructor of user defined exception is required to for unit tests of client code which calls your method and have to handle exception might be thrown. But if a constructor requires argument of custom type either it still could be difficult properly instantiate.
At the same time Microsoft guideline for user defined exceptions contradicts YAGNI and KISS principles and suggests to write and leave unused code. If you have no plan to serialize your exception why to make it serializable? Additional code requires additional tests and so on which increases support cost or/and reduces reliability.
Upvotes: 0