I have received an error log that shows the following code threw the exception in set
public double Value {
get {
switch (DefinedUnits.Distance) {
case DistanceUnits.Feet: { return Feet; }
case DistanceUnits.Meters: { return Meters; }
case DistanceUnits.NM: { return NauticalMiles; }
default: { throw new Exception("Invalid Distance Unit Specified"); }
}
}
set {
switch (DefinedUnits.Distance) {
case DistanceUnits.Feet: { Feet = value; break; }
case DistanceUnits.Meters: { Meters = value; break; }
case DistanceUnits.NM: { NauticalMiles = value; break; }
default: { throw new Exception("Invalid Distance Unit Specified"); }
}
}
}
DefinedUnits.Distance is an enum:
public enum DistanceUnits {
Meters,
Feet,
NM
}
There is no way I can see in my code that something else can be sent. I have no place where this enum is treated as an integer so that a bad value could be passed in. The user cannot tell me what he was doing. Or rather he tells me he was doing something that could not have called this.
Is there a logical explanation for why this happened and how can I stop it?
Thanks
DefinedUnits.Distance is either null or initialised to something out of range.
Changing your default handler should give you a clue:
default: { throw new Exception("Invalid Distance Unit Specified: " + DefinedUnits.Distance != null ? DefinedUnits.Distance.ToString() : '**null**' ); }