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

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

问题描述

考虑一些可以抛出已检查异常的代码(异常).当然,您的代码 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.

  • 消息是在抛出异常时创建的.因此,它最多只能提供非常低级别的信息,这可能不适合向用户报告.
  • 从哲学上讲,在我看来,使用该消息与异常的全部意义背道而驰,即将错误处理的检测和启动(throw 部分)与处理和报告的完成(<代码>捕获部分).使用消息意味着消息必须有利于报告,这将报告的责任转移到应该只负责检测和启动的位置.也就是说,我认为 Throwable 设计的 getMessage() 部分是一个错误.
  • 该消息未本地化.尽管它的名字,getLocalizedMessage() 并不是很好,因为你可能不知道你想要使用什么语言环境,直到你 catch 异常(是去系统的报告您的英语系统管理员读取的日志,还是会在 GUI 的法语用户的窗口中弹出?).
  • 我听说 Java 7 对 IOException 的异常层次结构有了很大改进,使您能够在不同的 catch 子句中处理不同类型的 I/O 错误,从而使 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.

而且我不是在询问消息文本的最佳实践".

And I am not asking about the "best practice" for the message text.

推荐答案

如果您向用户呈现错误情况,它可能应该是用户友好的消息.例外包含用户不应该/不需要知道的技术细节.

If you are presenting an error condition to the user, it should probably be a user-friendly message. Exceptions contain technical details that the user should not/does not need to know.

在某些情况下,显示堆栈跟踪信息可能是一个安全问题,因此永远不应向用户显示堆栈跟踪.

In some situations it could be a security concern to present stacktrace information, so the user should never be shown stack traces.

如果您向用户显示错误消息,在某些时候您会有意识地做出显示弹出窗口或向日志窗口添加消息的决定.此时,您可以将任何异常转换为更用户友好的消息.请注意,您可能需要比默认 Exception 类型提供的更多信息,因此您可以/应该创建自己的 Exception 类型,其中包含呈现所有信息所需的所有信息用户需要的数据.

If you are displaying error messages to the user, there is some point where you consciously make the decision to show a popup, or add a message to a log window. At that point you can translate any exception into a more user friendly message. Note that you might need more information than the default Exception types provide, so you can/should probably create you own Exception types that contain all the information you need to present all the data you need to the user.

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

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