您应该报告异常的消息文本吗? [英] Should you report the message text of exceptions?

查看:119
本文介绍了您应该报告异常的消息文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一些可以引发检查异常的代码(类型为 异常 )。当然,你的代码 catch 是异常。你不仅仅是吞下这个例外,你的代码也会以某种方式通过用户界面向用户报告。在日志文件中,或者使用GUI弹出窗口。

Consider some code that can throw a checked exception (an exception of type Exception). Your code catches the exception, of course. You don't just swallow the exception either, your code reports it in some way to the user through your user interface. In a log file, perhaps, or using a GUI pop-up.

如果向用户报告的文本包含异常的消息文本。也就是说,由 Throwable.getMessage() Throwable.getLocalizedMessage()

Should the text you report to the user include the message text of the exception. That is, the text provided by Throwable.getMessage() or Throwable.getLocalizedMessage()?

我想没有,但是似乎很多人不同意我的看法。那么我有什么错误?我的论点如下。

I think not, but it seems many disagree with me. So what have I got wrong? My argument is as follows.


  • 在抛出异常时创建了该消息。因此,它最多只能提供非常低级别的信息,这可能不适合向用户报告。

  • 哲学上,使用消息似乎反对整个例外情况,将处理和报告完成( catch 部分)的错误处理( throw 部分)的检测和启动分开)。使用该消息意味着该消息必须有助于报告,这将报告责任移交给仅负责检测和启动的位置。也就是说,我会认为 Throwable 的设计部分 getMessage()是一个错误。 li>
  • 消息未本地化。尽管它的名字, getLocalizedMessage()不是很好,因为你可能不知道你想要使用的区域设置,直到你 catch 异常(是由英文系统管理员读取系统日志的报告,还是在GUI的法国用户的窗口中弹出)?

  • 我听说Java 7对于 IOException 有非常大的改进的异常层次结构,使您可以处理不同类型的I / O错误,不同的 catch 子句,使 getMessage()文本不太重要。这意味着即使Java设计师对 getMessage()有些不舒服。

  • The message was created when the exception was thrown. It therefore at best can provide only very low level information, which can be inappropriate for reporting to a user.
  • Philosophically, using the message seems to me against the whole point of exceptions, which is to separate the detection and initiation of error handling (the throw part) from completion of handling and reporting (the catch part). Using the message means the message must be good for reporting, which moves responsibility for reporting to the location that should be responsible for only detection and initiation. That is, I'd argue that the getMessage() part of the design of Throwable was a mistake.
  • The message is not localised. Despite its name, getLocalizedMessage() is not much good because you might not know what locale you want to use until you catch the exception (is the report to go to a system log read by your English system administrators, or is it to pop up in a window for the French user of the GUI?).
  • I hear that Java 7 has a much improved exception hierarchy for IOException, enabling you to handle different kinds of I/O erors in diffferent catch clauses, making the getMessage() text less important. That implies even the Java designers are somewhat uncomfortable with getMessage().

我不是问报告栈跟踪是否有用。堆栈跟踪仅对建议错误的异常有用。也就是说,对于未经检查的异常。我认为在这种情况下,提供异常消息的低级细节不仅仅是有用的,而且是强制性的。但是我的问题涉及检查的异常,例如文件未找到。

I'm not asking whether reporting the stack-trace is useful. A stack-trace is only ever going to be useful for an exception that suggests a bug. That is, for an unchecked exception. I think in such circumstances providing the low-level detail of the exception message is not just useful but mandatory. But my question deals with checked exceptions, such as file-not-found.

推荐答案

不,不应该直接显示异常直接向用户发送错误消息,他们是低级别的技术细节,用户几乎总是希望更容易理解的东西,即使它不提供与堆栈跟踪一样多的信息!

No, exceptions shouldn't be shown directly in error messages directly to the user, they're low level technical details and the user almost always wants something more understandable, even if it doesn't provide as much information as a stack trace would!

我说几乎总是因为有些情况(如在IDE中),您可以认为您的用户在技术上有足够的能力来查看堆栈跟踪;确实在这种情况下,他们可能会更喜欢它的dumdown错误消息。

I say almost always because there are cases (such as in IDEs) where you can consider your users technically competent enough to look at stack traces; indeed in this case they will probably prefer it to a "dumbed down" error message.

然而,我个人认为,堆栈跟踪应该总是记录在用户可以访问的地方所以如果他们抱怨说程序不工作,你可以看到他们发送给你的文件。

However, personally I think stack traces should always be logged somewhere that the user can access so that if they complain that "the program isn't working" you can see exactly what went on if they send you that file.

这篇关于您应该报告异常的消息文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆