Reputation: 2666
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
Upvotes: 2
Views: 160
Reputation: 2172
If you use Dotfuscator (or some other obfuscating software) and you convert string value (for instance "Feet") to enum, then it is possible that enum names were obfuscated and string-to-enum conversion fails.
Upvotes: 0
Reputation: 3442
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**' ); }
Upvotes: 1
Reputation: 43321
Print to the log Exception.StackTrace and (int)DefinedUnits.Distance value to get more information.
Upvotes: 0
Reputation: 1501636
Perhaps the value was fetched before it was ever set, leaving a default value which was invalid? (Admittedly if you haven't set any corresponding integers, Meters
should correspond to 0...) You should adjust your code to:
InvalidOperationException
)If you have the stack trace, you should at least be able to see whether it was the getter or the setter that was throwing the exception...
Upvotes: 3