C# 缺失数据的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1453200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:41:35  来源:igfitidea点击:

Exception for missing data

c#exception

提问by Elz

I was wondering what kind of exception should one throw for missing data. For example if an xml node doesn't contain data. It would be easy to "throw new Exception(...)" but this is not recommended. Another option would be to create a new exception class like MissingDataExceptionor InvalidDataExceptionbut isn't there a built-in exception class for this case?

我想知道应该为丢失的数据抛出什么样的异常。例如,如果 xml 节点不包含数据。“ throw new Exception(...)”很容易,但不推荐这样做。另一种选择是创建一个新的异常类,如MissingDataExceptionInvalidDataException,但在这种情况下是否没有内置异常类?

采纳答案by Paul Turner

As a rule of thumb, check the existing .NET framework exceptions for a suitable exception to throw before deriving your own. To answer your question directly, there is no "missing data" exception currently available to throw, but that doesn't mean there aren't suitable exceptions to cover your situation.

根据经验,在派生您自己的异常之前,请检查现有的 .NET 框架异常以获取要抛出的合适异常。要直接回答您的问题,目前没有可抛出的“丢失数据”异常,但这并不意味着没有合适的异常来涵盖您的情况。

In your case, the humble InvalidOperationExceptionmay be suitable; this exception is thrown when you call a method on an object, but the object's state is not appropriate for the operation. Examples of this include calling methods on a closed stream and an enumerator that has passed the end of the collection. If the XML data is the internal state of an object, and a method call has discovered the bad data, InvalidOperationExceptionis a good candidate.

在你的情况下,谦虚InvalidOperationException可能是合适的;当您在对象上调用方法时抛出此异常,但该对象的状态不适合该操作。这方面的示例包括在关闭的流上调用方法和已通过集合末尾的枚举器。如果 XML 数据是一个对象的内部状态,并且一个方法调用发现了坏数据,InvalidOperationException则是一个很好的候选者。

If you are passing your XML data to a method, an ArgumentException, or one of its derivatives may be an appropriate choice. There is a small family of these exceptions, all indicating that an argument passed to a method is not as the method expected.

如果您将 XML 数据传递给方法,则ArgumentException或 其派生词之一可能是合适的选择。有一小部分这些异常,所有这些都表明传递给方法的参数与方法预期的不一样。

You will only want to create a custom exception when you want the exceptional circumstance to be handled differently from other exceptions. If you do choose to create your own exception, be sure to derive it from a higher exception than Exception, so that the nature of the exception is implied by the base class.

当您希望以与其他异常不同的方式处理异常情况时,您将只想创建自定义异常。如果您确实选择创建自己的异常,请确保从比 更高的异常派生它Exception,以便基类隐含异常的性质。

回答by Clement Herreman

You can use System.Xml.XmlException.

您可以使用System.Xml.XmlException.

Edit : Even if System.Xml.XmlExceptioncould fit, I think you should define your own exception, as it would be more precise, and you could describe what kind of data is missing : an id, a date, etc.

编辑:即使System.Xml.XmlException可以,我认为您应该定义自己的异常,因为它会更精确,并且您可以描述缺少的数据类型:ID、日期等。

回答by Keping

Do not call "throw new Exception", because you don't know how to handle the exception.

不要调用“throw new Exception”,因为您不知道如何处理异常。

Define your own exception. Be more specific, such as XMLDataMissingException. Then you can give a meamingful message to user or log it.

定义你自己的例外。更具体一些,例如 XMLDataMissingException。然后你可以给用户一个有趣的消息或记录它。

回答by Chuck Conway

throw new Exception("my message"); (or other built in Exception) is often the correct approach. The alternative is an explosion of Exception classes that may only get used once.

throw new Exception("我的消息"); (或其他内置异常)通常是正确的方法。另一种选择是可能只使用一次的异常类的爆炸式增长。

If new Exceptions are warranted they should be created in the context of the domain, not the problem.

如果需要新的例外,它们应该在域的上下文中创建,而不是在问题的上下文中。

回答by pitbull

As a rule of thumb you should throw exceptions in Exceptional Circumstances. If the data in question adversely affects the object's state or behaviour then throw a custom exception. An alternative approach might involve some kind of validator that fires events which your client handles gracefully, for example, report the error to end-user or insert default values.

根据经验,您应该在特殊情况下抛出异常。如果相关数据对对象的状态或行为产生不利影响,则抛出自定义异常。另一种方法可能涉及某种验证器,它会触发客户端正常处理的事件,例如,向最终用户报告错误或插入默认值。

I had a similar problem you described in which I had 2 clients (call them A & B) reading and modifying a single xml file. Client A deleted node X then Client B attempted to update node X. Clearly, updating a node that no longer exists is a problem. To solve this problem I took inspiration from SQL Server which reports the number of rows affected by an UPDATE statement. In this particular case I raised the UpdateNode event as normal with a number of rows affected property set to zero.

我遇到了您描述的类似问题,其中我有 2 个客户端(称为 A 和 B)读取和修改单个 xml 文件。客户端 A 删除了节点 X,然后客户端 B 尝试更新节点 X。显然,更新不再存在的节点是一个问题。为了解决这个问题,我从 SQL Server 获得灵感,它报告受 UPDATE 语句影响的行数。在这种特殊情况下,我像往常一样引发了 UpdateNode 事件,并将受影响的行数属性设置为零。

回答by Kirill Kovalenko

There is also System.Data.ObjectNotFoundExceptionclass which you may consider.

还有System.Data.ObjectNotFoundException你可以考虑的课程。

Update:As of Entity Framework 6, this exception class' fully qualified name is System.Data.Entity.Core.ObjectNotFoundException.

更新:从 Entity Framework 6 开始,这个异常类的完全限定名称是System.Data.Entity.Core.ObjectNotFoundException.

See this questionfor further details on EF5->EF6 namespace changes.

有关EF5->EF6 命名空间更改的更多详细信息,请参阅此问题