Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

The main reasons for using namespaces are the avoidance of 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 that naming convention. Thus adding an additional sub-namespace is just 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 enum into a sub-namespace of its own, then all helper structs, and so on. Actually, I don't think this would make real-world code any clearer or easier to navigate in, so its pretty useless.

share|improve this answer
    
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
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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