7

I was doing a code review and came across something odd which I've never seen before. The developer decided to create a sub-namespace just to contain all the assembly's exceptions.

I thought I had read that this was explicitly not a good use of namespaces and that exceptions should be kept at the "lowest" namespace from which they are thrown, but I haven't found anything on MSDN (or anywhere else for that matter). At the same time, it seems odd that I haven't come across this before in the framework itself or the third party libraries I've used.

Are there any guidelines exist that would push away from keeping all exceptions in a .Exceptions sub-namespace?

22

The two main reasons for using namespaces are to avoid name collisions, and to add logical grouping. But since exception class names typically have the form "XYZException" (and other classes have not), those two requirements are already fulfilled by this naming convention. Thus adding an additional sub-namespace is unneccessary in a context where everybody follows the convention. It tries to solve a problem which is already solved, adding a needless complexity to the code.

Moreover, to my experience its better to use namespaces for partioning under aspects of your domain, not for technical grouping. When you start to put exception classes into a sub-namespace, the next decision will be to put all enums into a sub-namespace of its own, then all helper structs, and so on. Actually, I don't think this makes code any clearer or easier to navigate in, so I do not think it would be useful.

  • I could imagine doing it if you had more than 12 or so exceptions, just for the sake of keeping your namespaces clean, but usually one namespace shouldn't have that many custom exceptions in the first place, so +1 for sure. – Magus Mar 17 '14 at 14:40
  • I agree with this line of reasoning but I'm hoping for something more authoritative, otherwise I'm sure others will have their own reasons to support it. – Andy Mar 17 '14 at 22:46
  • 4
    @Andy: The only "authoritative" article from MS I could find is this one msdn.microsoft.com/en-us/library/893ke618%28v=vs.71%29.aspx, and it doesn't recommend or forbid the usage of sub-namespace for exceptions. And I think its often better not to ask for a "higher authority" to make a decision, but to trust common sense and your own judgement. – Doc Brown Mar 18 '14 at 7:15
  • I've seen that link as well. As far as common sense and judgment, at my job many an hour has been spent debating similar issues. I'm a bit surprised how often there are strong opinions on both sides. – Andy Mar 18 '14 at 21:08
  • I agree with this, in a lot of MVC frameworks they have a /controllers and /models folders, going against the grain I like to put the two in the same spot logically grouped by functionality. – Matthew Jan 31 '17 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.