异常中的错误代码与异常层次结构
您认为在异常中使用错误代码来指定错误类型可以吗? 请看一下这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
时,错误代码非常有用
所以,大多数时候,我没有看到任何添加错误代码中的值。我更喜欢异常层次结构,或者至少是在日志文件中找到真正有用的清晰错误消息(即使在程序员离开公司两年后)。
如果您对错误代码有要求 - 解决方案还不错。考虑在中央存储库(属性文件)中收集所有错误代码,以便您可以轻松交换完整的错误代码集:
Error codes are useful when
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:
根据我的经验,异常代码主要用作用户的信息消息。
我什至没有看到有人尝试解析一般异常消息,以便根据错误代码做出不同的反应,通常是通过异常层次结构完成的。< 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.
我通常结合使用两者。
您需要对异常进行分类并做出设计决策。
例如,您可以使用异常来源、类型、影响和处理等参数来对异常进行分类。如果异常属于同一类别,请使用其中的错误代码。对属于不同类别的异常使用层次结构。
如果您选择异常处理作为重要参数,则可以根据您想要的处理方式在两个选项之间进行选择:
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:
从内存和时间方面来看,从性能角度创建复杂异常层次结构的堆栈跟踪非常昂贵,因此,如果您为可以通过 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.
如果您想根据导致异常的原因(无效的名称或无效的 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.