从枚举中获取异常
我收到一条错误日志,显示以下代码在 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
}
我无法在代码中看到可以发送其他内容。我没有地方将此枚举视为整数,以便可以传入错误值。用户无法告诉我他在做什么。或者更确切地说,他告诉我他正在做一些不可能称之为这样的事情。
对于为什么会发生这种情况有一个合乎逻辑的解释吗?我该如何阻止它?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许该值在设置之前就已获取,留下了无效的默认值? (诚然,如果您没有设置任何相应的整数,
Meters
应该对应于 0...)您应该将代码调整为:InvalidOperationException
)如果您有堆栈)踪迹,你至少应该能够看出是 getter 还是 setter 抛出了异常......
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...
DefinedUnits.Distance 为 null 或初始化为超出范围的值。
更改默认处理程序应该会给您一个线索:
default: { throw new Exception("指定的距离单位无效: " + DefinedUnits.Distance != null ? DefinedUnits.Distance.ToString() : '**null**' ); }
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**' ); }
打印到日志 Exception.StackTrace 和 (int)DefinedUnits.Distance 值以获取更多信息。
Print to the log Exception.StackTrace and (int)DefinedUnits.Distance value to get more information.
如果您使用 Dotfuscator(或其他一些混淆软件)并将字符串值(例如“Feet”)转换为枚举,则枚举名称可能会被混淆,并且字符串到枚举的转换会失败。
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.