Suppose we have a method that accepts a value of an enumeration. After this method checks that the value is valid, it switch
es over the possible values. So the question is, what is the preferred method of handling unexpected values after the value range has been validated?
For example:
enum Mood { Happy, Sad }
public void PrintMood(Mood mood)
{
if (!Enum.IsDefined(typeof(Mood), mood))
{
throw new ArgumentOutOfRangeException("mood");
}
switch (mood)
{
case Happy: Console.WriteLine("I am happy"); break;
case Sad: Console.WriteLine("I am sad"); break;
default: // what should we do here?
}
What is the preferred method of handling the default
case?
- Leave a comment like
// can never happen
Debug.Fail()
(orDebug.Assert(false)
)throw new NotImplementedException()
(or any other exception)- Some other way I haven't thought of