简体   繁体   English

在实现接口时,是否需要处理文件化异常的引发问题

[英]Do I need to handle raising of documented exceptions when implementing interfaces

I'm creating a custom C# collection by implementing ICollection in my class and have noticed some of the methods defined by the interface have exceptions documented ie http://msdn.microsoft.com/en-us/library/0efx51xw(v=vs.110).aspx 我正在通过在我的类中实现ICollection创建一个自定义C#集合,并注意到该接口定义的一些方法具有异常记录,即http://msdn.microsoft.com/en-us/library/0efx51xw(v=vs 0.110)的.aspx

None of the examples or documentation I've seen for implementing interfaces seem to talk about needing to implement raising exceptions. 我见过的用于实现接口的示例或文档似乎都没有谈到需要实现引发异常的问题。 Whenever the documentation for a method in an interface shows exceptions could be raised by the method should I be checking for each of these conditions and raising the exceptions within my implementation code? 每当接口中的方法文档显示该方法可能引发异常时,我应该检查每个条件并在我的实现代码中引发异常吗?

When documenting an interface, it is common to include xml documentation to indicate the exceptions that may be thrown by implementations. 在记录接口时,通常包含xml文档以指示实现可能引发的异常。 This doesn't mean that implementations MUST be able to throw these, just that they MAY throw them. 这并不意味着实现必须能够抛出这些,只是它们可能抛出它们。

See Liskov's Substitution Principle. 见Liskov的替代原则。 Specifically "No new exceptions should be thrown by methods of the subtype, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the supertype." 具体而言“子类的方法不应抛出新的异常,除非这些异常本身是超类型方法抛出的异常的子类型。” - http://en.wikipedia.org/wiki/Liskov_substitution_principle - http://en.wikipedia.org/wiki/Liskov_substitution_principle

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

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