简体   繁体   English

当我期望相等的两个值不相等时抛出什么异常? (C#)

[英]What exception to throw when two values I expect to be equal are not equal? (C#)

I feel like this has to be a duplicate, but I've tried looking around and can't find what I'm looking for. 我觉得这必须是重复的,但我试过环顾四周,找不到我想要的东西。

The example I'm working with starts with parsing an XML document. 我正在使用的示例从解析XML文档开始。 In this XML document the name of a person is stated in two different locations. 在此XML文档中,人员的姓名在两个不同的位置陈述。 Later in a different method, I need to use this person's name. 后来用另一种方法,我需要使用这个人的名字。 I can use any of these two references to the person's name in the XML document since they are the same, or so I expect. 我可以在XML文档中使用这两个引用人名的任何一个,因为它们是相同的,或者我希望如此。 I first want to check that they are indeed the same. 我首先要检查它们是否确实相同。 If they are not equal for whatever reason, I feel like it would be best to throw an exception. 如果由于某种原因它们不相等,我觉得最好抛出异常。 Is there an exception for when two values that one would expect are equal are not in fact equal? 当一个人期望的两个值相等时实际上是不相等的,是否存在例外情况?

I considered ArgumentException , but I'm not sure if that's best since it specifies: 我考虑过ArgumentException ,但我不确定它是否最好,因为它指定:

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. 调用方法并且至少有一个传递的参数不符合被调用方法的参数规范时,抛出ArgumentException。

This isn't the case here since the arguments are fine, it's just that the value of one of the properties (ie the person's name) is not what I expect it to be. 这不是这里的情况,因为参数很好,只是其中一个属性(即人名)的值不是我所期望的。

I suppose the first question you might have is why do I want to throw an exception. 我想你可能遇到的第一个问题是我为什么要抛出异常。 Maybe it's not the best option, but I feel it should be done considering that the reason the two names do not match is because when the XML document was created one of the name-writes did not do its job correctly, which I'd probably want to know. 也许这不是最好的选择,但我认为应该考虑到这两个名称不匹配的原因是因为当创建XML文档时,其中一个名称写入没有正确完成其工作,我可能想知道。 I'm not experienced with error handling, so it's possible that this doesn't mean an exception should be thrown. 我没有错误处理的经验,所以这可能并不意味着应该抛出异常。 Any advice would be appreciated. 任何意见,将不胜感激。

I first want to check that they are indeed the same. 我首先要检查它们是否确实相同。 If they are not equal for whatever reason, I feel like it would be best to throw an exception. 如果由于某种原因它们不相等,我觉得最好抛出异常。

You could use InvalidOperationException , since your operation expects the values to be equal. 您可以使用InvalidOperationException ,因为您的操作期望值相等。 If the values are not equal, your operation is invalid. 如果值不相等,则操作无效。

You could also define your own exception. 您还可以定义自己的例外。 You can afterward customize your exception in regards of your situation / objects to compare. 您可以随后根据您的情况/对象自定义您的例外以进行比较。

Instead of an exception, this should be a code contract : your code couldn't work if input XML has provided name twice. 这应该是代码合同而不是异常:如果输入XML提供了两次名称,则代码无法工作。 It's a requirement of your code to work properly . 这是您的代码正常工作的要求

Thus, I would create a List<string> at the beginning of your method, and I would accumulate found names in the XML. 因此,我将在方法的开头创建一个List<string> ,并在XML中累积找到的名称。 Once you need to use the whole name, I would check the list with a code contract: 一旦您需要使用整个名称,我将使用代码合同检查列表:

Contract.Assert(names != null && names.Count > 0);
Contract.Assert(names.Skip(1).All(name => name == names[0]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM