Organizing custom exceptions in C #

I create a C # application and try, when necessary, to use custom exceptions. I looked at other issues here and in the MSDN Design Guide, but did not find anything specific, like what I'm interested in here.

What is the best practice for organizing custom exceptions?

For example, I have a Disk class that throws an InvalidDiskException . Disk is the only class that throws this exception.

I currently have an exception attached to my Disk.cs file as follows:

Disk.cs

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OrganizingExceptionsInCSharp { class Disk { [Serializable] public class InvalidDiskException : Exception { public InvalidDiskException() { } public InvalidDiskException(string message) : base(message) { } public InvalidDiskException(string message, Exception innerException) : base(message, innerException) { } } // // Code that throws the exception. // } } 

Should an exception be defined at the same level as the disk (i.e. not nested inside)? Should the exception be nested in Disk but save its own partial file? Could there be other better options? Please let me know if there are other considerations that I did not think about.

+7
source share
2 answers

I would define my custom exceptions in {MyNamespace}. Exceptions

0
source

User exceptions, IMHO, must be in their own independent assembly for one simple reason: if you throw them at the borders of a process / machine, each process may run into problems (and therefore another exception) t share a common understanding of user exceptions.

-2
source

All Articles