I have a class that can throw a very rare exception that is very particular to that class.
I want to create a custom exception for it, but I ponder about the best location for this class.
The way I see it, my 3 options are:
// Nested
namespace Cubusky.Collections.Generic
{
public class ObserverSet<T>
{
public class NonexistentUnsubscriberException : Exception
{
// ...
}
}
}
// Same Namespace
namespace Cubusky.Collections.Generic
{
public class ObserverSet<T>
{
// ...
}
public class NonexistentUnsubscriberException : Exception
{
// ...
}
}
// Different Namespace
namespace Cubusky.Collections.Generic
{
public class ObserverSet<T>
{
// ...
}
}
namespace Cubusky
{
public class NonexistentUnsubscriberException : Exception
{
// ...
}
}
From what I understand about your question, I would prefer the nested one. The advantages are for the nested approach:
ObserverSet<T>
class, making it clear that this exception is specific to this class.However disadvantages for that are:
ObserverSet<T>
class.ObserverSet<T>
class more complex and harder to read.On the other hand, one will also find pros and cons for the other two approaches.
Advantages:
ObserverSet<T>
class but is more accessible.Disadvantages:
ObserverSet<T>
compared to the nested approach.Advantages:
Disadvantages:
ObserverSet<T>
.ObserverSet<T>
.From what I understand, you should go with the nested approach. This is due to its uniqueness of this exception. However, if you have several unique exception or need this exception later on for a different class, one would find charm in the other two options as well.