异常中的错误代码与异常层次结构

发布于 2025-01-05 18:30:43 字数 559 浏览 0 评论 0原文

您认为在异常中使用错误代码来指定错误类型可以吗? 请看一下这段代码:

public class MyException extends Exception {
    public static final String ERROR_CODE_INVALID_NAME = "";
    public static final String ERROR_CODE_INVALID_ID = "";
    ...

    private String errorCode;

    public MyException(String message, String errorCode) {
        super(message);
        this.errorCode = errorCode;
    }

    public String getErrorCode() {
        return errorCode;
    }
}

我知道在这个例子中最好使用枚举而不是字符串,但我实际上担心错误代码的概念。您认为异常层次结构在这里会更好吗?我找不到任何权威来源表明异常中的错误代码是反模式。 谢谢。

Do you think it is ok to use error codes within exception to specify error type?
Please take a look on this code:

public class MyException extends Exception {
    public static final String ERROR_CODE_INVALID_NAME = "";
    public static final String ERROR_CODE_INVALID_ID = "";
    ...

    private String errorCode;

    public MyException(String message, String errorCode) {
        super(message);
        this.errorCode = errorCode;
    }

    public String getErrorCode() {
        return errorCode;
    }
}

I know that it is better to use enum instead of Strings in this example, but I'm actually concerned about the concept of error codes. Do you think exceptions hierarchy would be better here? I can't find any authoritative source that says error codes within exception is anti-pattern.
Thx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

微暖i 2025-01-12 18:30:43

时,错误代码非常有用

  • 当您无法显示完整的错误消息(洗碗机显示)
  • ,必须在内部处理代码(如果出现特定代码或服务器在客户端运行时向客户端发送错误代码,则会触发某些逻辑)负责消息)
  • 我们有一个很棒的手册,用户可以使用代码来获取全面的信息
  • 用户不需要知道发生了什么,但必须联系供应商

所以,大多数时候,我没有看到任何添加错误代码中的值。我更喜欢异常层次结构,或者至少是在日志文件中找到真正有用的清晰错误消息(即使在程序员离开公司两年后)。

如果您对错误代码有要求 - 解决方案还不错。考虑在中央存储库(属性文件)中收集所有错误代码,以便您可以轻松交换完整的错误代码集:

myexception.ERROR_CODE_INVALID_NAME=text or number
myexception.ERROR_CODE_INVALID_ID=text or number

Error codes are useful when

  • you can't display a complete error message (dish washer display)
  • the code has to be processed internally (some logic is triggered if a certain code appears or a server sends an error code to the client while the client is responsible for the message)
  • we have a great manual and the user can use the code to get comprehensive information
  • The user does not need to know, what happend, but has to contact the vendor

So, most time, I don't see any added value in error codes. I prefer an exception hierarchy or at least clear error message that are really useful, when found in a logfile (even 2 years after the programmer has left the company).

If you have requirements for error codes - the solution is not bad. Consider collecting all error codes in a central repository (a properties file) so that you can exchange the complete set easily:

myexception.ERROR_CODE_INVALID_NAME=text or number
myexception.ERROR_CODE_INVALID_ID=text or number
倾城泪 2025-01-12 18:30:43

根据我的经验,异常代码主要用作用户的信息消息。

我什至没有看到有人尝试解析一般异常消息,以便根据错误代码做出不同的反应,通常是通过异常层次结构完成的。< br>

从另一方面来说,为每个特定情况创建新的异常子类然后使用异常代码可能很困难。
例如,如果对于用户代码来说,它不计量事务失败的原因,它应该以任何方式回滚它,但对于最终用户来说,它发生的原因很重要(错误的参数、数据库连接或其他)。

所以,总结一下,如果您期望以不同的方式来处理不同的情况,那么最好使用不同的异常类型,但是如果您应该以相同的方式处理几个问题但只通知用户特定原因,那么使用异常代码会更容易。

From my experience exception codes are used mostly as information message for user.

I didn't saw even once that somebody try to parse general exception message in order to react differently depends on error code, usually it's done via exception hierarchy.

From another hand it could be hard to create new exception subclass for every particular case and then exception codes are used.
For example, if for user code it doesn't meter why transaction failed, it should rollback it any way, but for end user it's important why it happened (wrong params, database connection or else).

So, to summarize, if you'r expecting different ways to handle different situations it's better to use different exception types, but if you should handle few problems in the same way but only notify user about particular cause it's easier to use exception codes.

巷子口的你 2025-01-12 18:30:43

我通常结合使用两者。

您需要对异常进行分类并做出设计决策。

例如,您可以使用异常来源、类型、影响和处理等参数来对异常进行分类。如果异常属于同一类别,请使用其中的错误代码。对属于不同类别的异常使用层次结构。

如果您选择异常处理作为重要参数,则可以根据您想要的处理方式在两个选项之间进行选择:

  1. 如果您想在一个 catch 块中捕获所有类型并以通用方式处理它们,请使用错误代码。
  2. 如果您想一次捕获特定类型并相应地处理它们,请使用层次结构。

I usually use a combination of both.

You need to categories your exceptions and make a design decision.

For example, you may use parameters such as source of exception, type, impact and handling to categorize your exception. If the exceptions fall in same category, use error codes within. Use hierarchy for the exception falling in different categories.

If you chose Exception Handling an important parameter, you may choose between the two options based on how you want to handle them:

  1. Use error codes if you want to catch all types in one catch block and handle them in a generic way.
  2. Use hierarchy if you want to catch specific type at a time and handle them accordingly.
酒几许 2025-01-12 18:30:43

从内存和时间方面来看,从性能角度创建复杂异常层次结构的堆栈跟踪非常昂贵,因此,如果您为可以通过 3-4 个静态错误代码解决的问题创建复杂的自定义异常层次结构...我更喜欢错误代码选项。一般来说,我更喜欢使用运行时异常(未在方法签名中检查),捕获检查异常的防御方法在我看来已经过时了。

Performance-wise creating a stacktrace of complex exception hierarchy is very expensive from both memory and time aspects, so if you create a complex custom exception hierarchy for something that you can solve by 3-4 static error codes... I would prefer the error code option. In general I prefer working with Runtime exceptions (not checked in method signature) the deffensive approach of catching checked exceptions is little out-dated IMO.

你的呼吸 2025-01-12 18:30:43

如果您想根据导致异常的原因(无效的名称或无效的 id)做出不同的反应(在代码中),那么我建议使用不同的异常。

如果没有,那么您甚至不需要 getErrorCode() 方法,您只需将错误代码添加到异常消息中,异常就会为您提供调试所需的所有信息。

If you want to react differently (in code) depending on what caused the exception (either invalid name or invalid id) then I would suggest having different exceptions.

If not, then you don't even need the getErrorCode() method, you can just add the error code to the message of the exception and the exception will give you all the information you need for debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文