简体   繁体   English

异常处理模式

[英]Exception handling pattern

It is a common pattern I see where the error codes associated with an exception are stored as Static final ints. 这是我看到的常见模式,其中与异常关联的错误代码存储为静态最终整数。 when the exception is created to be thrown, it is constructed with one of these codes along with an error message. 当创建要抛出的异常时,它将使用这些代码之一以及错误消息构造。 This results in the method that is going to catch it having to look at the code and then decide on a course of action. 这导致该方法要抓住它必须查看代码然后决定一个行动方案。

The alternative seems to be- declare a class for EVERY exception error case (although related exceptions would derieve from a common base class ) 替代似乎是 - 为每个异常错误情况声明一个类(虽然相关的异常将从一个公共基类中解除)

Is there a middle ground ? 有中间地带吗? what is the recommended method ? 推荐的方法是什么?

This is a good question. 这是一个很好的问题。 I believe there is definitely a middle ground. 我相信肯定存在中间立场。

Error codes are essential in my opinion for displaying errors to QA, and for customers to report to customer support and back to developers. 在我看来,错误代码对于向QA显示错误以及客户向客户支持报告并返回给开发人员至关重要。

For programmatically handling errors I personally don't recommend error codes, I'd recommend a new Class for each category of errors, but definitely not for every single error. 为了以编程方式处理错误,我个人不建议使用错误代码,我建议为每类错误推荐一个新的类,但绝对不会针对每个错误。 Java did a decent job getting us started with Exceptions like IOException, IllegalArgumentException, UnsupportedOperationException, etc. I frequently throw and catch these when appropriate in my code. Java做了一个不错的工作,让我们开始使用IOException,IllegalArgumentException,UnsupportedOperationException等异常。我经常在我的代码中抛出并捕获这些异常。

If you have a new category of exceptions that your code should respond to programmatically then you should definitely create a new class for it, extending the appropriate parent class. 如果您的代码应该以编程方式响应的新类别,那么您应该为它创建一个新类,扩展相应的父类。 For example UserRegistrationException or ProductException. 例如UserRegistrationException或ProductException。

If you can generalize the behavior across different error cases, especially if such behavior can be classified into "classes" of behavior (no pun intended), having an exception class hierarchy makes sense. 如果你可以在不同的错误情况下概括行为,特别是如果这种行为可以归类为行为的“类”(没有双关语),那么具有异常类层次结构是有意义的。

If you must catch EVERY (or almost) every exception anyway, and the handling of most of them is nearly identical (eg print error and quit), having 1 class with exception number in it makes sense as it's a simpler design. 如果你必须捕获每一个(或几乎)每个异常,并且它们中的大多数的处理几乎相同(例如打印错误和退出),那么有一个带有异常编号的类是有意义的,因为它是一个更简单的设计。

To answer your specific question: Your decision should be based on how and why your exceptions will be processed. 回答您的具体问题:您的决定应基于处理异常的方式和原因。 Do you want to make your program as foolproof as possible and conveniently react to every possible error scenario individually? 您是否希望尽可能简化您的程序并方便地对每个可能的错误情况做出反应? Then you should indeed create an Exception class for every possible error cause you can identify. 那么你确实应该为每个可能的错误创建一个Exception类,因为你可以识别。 Otherwise, it is best to decide for each case individually. 否则,最好单独决定每个案例。

There are a number of very common errors that jeopardize the stability of your entire program (such as ClassNotFoundException or NoSuchMethodException) - these should definitely be handled specifically. 有许多非常常见的错误会危害整个程序的稳定性(例如ClassNotFoundException或NoSuchMethodException) - 这些错误肯定应该专门处理。 There are other errors that are closely related to one another, resulting in similar problems - these can very well be grouped or organized hierarchically (IOException stands for all kinds of errors related to in- or output, NetworkIOException stands only for in- and output errors related to network access, etc.). 还有其他错误彼此密切相关,导致类似的问题 - 这些问题可以很好地分层组织或组织(IOException代表与输入或输出相关的各种错误,NetworkIOException仅代表输入错误和输出错误与网络访问等有关)。

Far more important than the exception's name and class hierarchy, in my opinion, is what you do with it: Where do you place your try/catch blocks? 在我看来,远比异常的名称和类层次更重要的是你用它做什么:你在哪里放置你的try / catch块? Which log entries should be written for which log file? 应该为哪个日志文件写入哪些日志条目? Who should be notified? 谁应该收到通知? Which error messages should be presented only to admins/developers? 哪些错误消息只应呈现给管理员/开发人员? Which errors should be communicated to the end user? 哪些错误应该传达给最终用户?

There are many patterns for handling exceptions, answering all sorts of questions like these. 有许多处理异常的模式,回答各种类似的问题。 A quite extensive collection of common and useful patterns can be found here . 这里可以找到相当广泛的常见和有用模式集合。

The "middle ground", as I see it, is to use the internal "error codes" (common pattern, but not VERY common, IMO) as additional information for reporting/informational purposes; 我认为,“中间立场”是使用内部“错误代码”(常见模式,但非常常见,IMO)作为报告/信息目的的附加信息; but not for deciding who catches the exception (this is determined by the exception class). 但不是用于决定谁捕获异常(这由异常类决定)。

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

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