简体   繁体   English

自定义流可以引发自己的自定义异常,还是仅应引发IOException?

[英]Can a custom stream throw its own custom exceptions or should it only throw IOException?

I have my own Connection class and a ConnectionStream class which essentially just wraps the Connection's send/receive methods. 我有我自己的Connection类和一个ConnectionStream类,该类实际上只是包装Connection的send / receive方法。

The Connection send/receive methods can throw exceptions like ServerClosedConnException or NetworkShutdownException 连接的发送/接收方法可以引发诸如ServerClosedConnException或NetworkShutdownException之类的异常

Should my Stream be catching these and wrapping them in IOException (using innerException), or can it just let them bubble up to the user (with some try-finally to handle cleanup ofcourse) 我的Stream是否应该捕获这些并将它们包装在IOException中(使用innerException),还是可以让它们冒泡给用户(可以通过一些尝试最终来处理课程)

I see in the .NET framework, the NetworkStream wraps any errors from the Socket in an IOException 我在.NET框架中看到,NetworkStream将来自Socket的所有错误包装在IOException中

Don't catch exceptions unless you intend to handle them. 除非您打算处理异常,否则不要捕获异常。 They will bubble up the call stack until an upstream handler catches and handles them, or the program terminates. 它们将使调用堆栈冒泡,直到上游处理程序捕获并处理它们,或者程序终止。

If your Connection class is throwing a very specific exception like ServerClosedConnException or NetworkShutdownException , and you catch that and rethrow a less specific IOException , I don't see how that is adding any value. 如果您的Connection类抛出非常特定的异常(例如ServerClosedConnExceptionNetworkShutdownException ,并且您捕获了该异常并重新抛出了一个不太特定的 IOException ,那么我看不到它是如何添加任何值的。

The best practice is to let exceptions bubble up unless you have more details to add or you can handle the exception yourself. 最佳实践是让异常冒出来, 除非您要添加更多详细信息, 否则您可以自己处理异常。 In the first case your throw a new exception with the actual exception as the InnerException . 在第一种情况下,您将抛出一个新异常,而实际异常为InnerException

Read Wrapping Exceptions in MSDN. 阅读MSDN中的包装异常

In your case I would let it just bubble up. 在您的情况下,我会让它冒出来。

The Stream base class acts as the defacto contract for its subclasses. Stream基类充当其子类的实际合同。 If you are planning on exposing your subclass in a public API, it would be considerate to abide by the documented behaviour for the Stream class, since the user of your subclass might be blissfully unaware of its particular type. 如果您打算在公共API中公开您的子类,则考虑遵守Stream类的已记录行为,因为子类的用户可能会很不了解其特定类型。 For example, if you wish to abide by the contract of the Read method, your implementation shouldn't be exposing any exceptions that aren't of a type listed at http://msdn.microsoft.com/en-us/library/system.io.stream.read . 例如,如果您希望遵守Read方法的约定,则您的实现不应公开任何非http://msdn.microsoft.com/zh-cn/library/中列出的类型的异常。 system.io.stream.read

However, this doesn't mean that you can't use more specific exception types. 但是,这并不意味着您不能使用更特定的异常类型。 If you have reason to create a custom exception type anyway (assuming you think this is a good idea after reading the design guidelines mentioned by JerKimball), it could potentially be a subclass of IOException. 如果您仍然有理由创建自定义异常类型(假设您阅读JerKimball提到的设计指南后认为这是一个好主意),则它可能是IOException的子类。 However, this probably shouldn't be necessary unless users of your Stream subclass are likely to want to do something besides logging with the exception details. 但是,除非您的Stream子类的用户除了使用异常详细信息进行日志记录之外还可能要执行其他操作,否则这可能不是必需的。

Here's what the Exception Guidelines say: 以下是例外准则的内容:

[ref] [参考]

The most relevant portion: 最相关的部分:

Do throw the most specific (the most derived) exception that is appropriate. 
For example, if a method receives a null (Nothing in Visual Basic) argument, 
it should throw System.ArgumentNullException instead of its base type 
System.ArgumentException.

I would extrapolate that to also mean in your case, the most detailed exception is the "correct" one to propogate; 我推断这也意味着在您的情况下,最详细的例外是“正确”的提议; making a few assumptions about your exception types, they are probably the "correct" one to propogate, possibly wrapped in a "ConnectionException" custom exception. 对异常类型进行一些假设,它们可能是“正确的”建议, 可能包含在“ ConnectionException”自定义异常中。

That said, I do see how it would "make sense" to consider wrapping it in an IOException , since that is the "shape" of the exception; 也就是说,我确实知道考虑将其包装在IOException中是“有意义的”,因为那是异常的“形状”; but in cases where I disagree with the guidelines and I go my way, it usually bites me later down the road. 但是,如果我不同意指导方针,却一意孤行,这通常会在以后给我带来麻烦。 :) :)

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

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