简体   繁体   English

创建自定义异常还是使用内置异常?

[英]Create custom exception or use built-in exceptions?

Currently I'm in the process of writing a client class that utilizes DNS, Sockets, and SSL among other classes that love to throw exceptions. 目前,我正在编写一个客户端类,在其他喜欢抛出异常的类中使用DNS,套接字和SSL。 Other people will be implementing this class, so I was wondering what the best practice is for throwing exceptions. 其他人将实现这个类,所以我想知道抛出异常的最佳做法是什么。

Should I create my own custom exception so they know that it is my class throwing the exception or should I allow the classes and methods I call (DNS, Sockets, etc.) to throw their own exceptions? 我应该创建自己的自定义异常,以便他们知道我的类抛出异常,还是应该允许我调用的类和方法(DNS,套接字等)抛出自己的异常? Currently, the code is in the hundreds of lines and growing with many different method calls. 目前,代码有数百行,并且随着许多不同的方法调用而增长。 What is the best practice for throwing exceptions in this situation? 在这种情况下抛出异常的最佳做法是什么?

If the BCL contains classes that already convey the meaning you want ( ArgumentNullException , for instance), use those. 如果BCL包含已传达所需含义的类(例如ArgumentNullException ),请使用这些类。

Reserve using your own exception classes for things that are specific to your API. 使用您自己的异常类保留特定于API的内容。

If you feel that you can add information, by all means raise your own exception but do not swallow exceptions - propagate them up as inner exceptions of your own. 如果您认为可以添加信息,请务必提出自己的异常但不要吞下异常 - 将它们作为您自己的内部异常传播。

It is fine to throw your own exceptions if they add information, but don't swallow the exceptions from the underlying services unless there is a security concern. 如果添加信息,可以抛出自己的异常,但除非存在安全问题,否则不要从底层服务中吞下异常。

If you are catching one exception and and throwing a new one, assign the old one to the InnerException property of the new one. 如果要捕获一个异常并抛出一个新异常,请将旧InnerException分配给新InnerException属性。 You can nest as many Exceptions as necessary in this way, which creates a hierarchical "view" into how the exception propagated, which is very helpful for debugging. 您可以通过这种方式嵌套尽可能多的异常,从而创建异常传播方式的分层“视图”,这对调试非常有用。

The worst thing you can do is throw an ApplicationException with details in the message string. 您可以做的最糟糕的事情是抛出一个ApplicationException,其中包含消息字符串中的详细信息。 IF you find yourself needing to do that, it's time for a custom exception. 如果您发现自己需要这样做,那么就应该进行自定义异常了。

It really depends on your audience, ie the consumers of your class. 这实际上取决于您的受众,即您班级的消费者。

For example, if you are essentially wrapping a lot of different exceptions, it may be a good idea to create custom exceptions that would simplify the error handling in the consumer. 例如,如果您实际上包含了许多不同的异常,那么创建自定义异常可能是一个好主意,这将简化消费者中的错误处理。

If you do create custom exceptions, be sure to include the original exception into the InnerException of your custom exception, unless you explicitly have a reason to hide it. 如果您确实创建了自定义异常,请确保将原始异常包含在自定义异常的InnerException中,除非您明确有理由隐藏它。 This will people using your class the most information available, as well as cover you if an exception comes back that your class doesn't completely cover. 这将是人们使用您的课程提供的最多信息,以及如果您的课程没有完全覆盖的例外情况,则会为您提供保险。

If you want anyone to catch and recover from your exceptions, it's probably best to use a small hierarchy of custom exception types, probably organized by the expected degree of recoverability (eg have an exception type indicating 'something unexpected happened, but the socket is probably still good', another for 'the socket state cannot be trusted, but starting over with a new socket connection might work', and another for 'the host says what you're doing won't work; don't even bother retrying unless you have reason to believe something has changed'). 如果你想让任何人从你的异常中捕获并恢复,最好使用一小部分自定义异常类型,可能是由预期的可恢复性程度组织的(例如,有一个异常类型指示'意外发生的事情,但套接字很可能仍然很好',另一个'套接字状态不能被信任,但是重新开始使用新的套接字连接可能有效',而另一个用于'主机说你正在做什么将无法工作;甚至不打扰重试除非你有理由相信某些事情发生了变化')。 The particulars of what caused an exception are often less important to the 'catching' code than the nature of the violated post-conditions. 引起异常的细节通常对于“捕获”代码而言不如违反后置条件的性质重要。

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

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