您对异常消息使用什么样式?

时间:2022-03-30 21:11:20

In writing the code that throws the exception I asked about here, I came to the end of my message, and paused at the punctuation. I realized that nearly every exception message I've ever thrown probably has a ! somewhere.

在编写抛出我在这里询问的异常的代码时,我走到了我的消息的末尾,并在标点符号处停了下来。我意识到我几乎所有抛出的异常消息都可能有!某处。

throw new InvalidOperationException("I'm not configured correctly!");
throw new ArgumentNullException("You passed a null!");
throw new StupidUserException("You can't divide by 0!  What the hell were you THINKING???  DUMMY!!!!!");

What tone do you take when writing exception messages? When going through logs, do you find any certain style of message actually helps more than another?

在编写异常消息时你会采取什么样的语气?在浏览日志时,您是否发现任何特定风格的消息实际上比另一种更有帮助?

9 个解决方案

#1


4  

A conversational tone in system messages makes the software look unprofessional and sloppy. Exclamation points, insults, and slang don't really have a place in polished exception messages.

系统消息中的对话音使得软件看起来不专业且不马虎。感叹号,侮辱和俚语在抛光的异常消息中确实没有位置。

Also, I tend to use different styles in Java for runtime exceptions and checked exceptions, since runtime exceptions are addressed to the programmer that made the mistake. Since runtime exceptions might be displayed to end users, I still "keep it clean," but they can be a little more terse and cryptic. Checked exception messages should be more helpful, since it may be that the user can fix the problem if you describe it (e.g., file not found, disk full, no route to host, etc.).

此外,我倾向于在Java中使用不同的样式来处理运行时异常和检查异常,因为运行时异常是针对造成错误的程序员的。由于运行时异常可能会显示给最终用户,我仍然“保持清洁”,但它们可能会更加简洁和神秘。检查的异常消息应该更有帮助,因为如果您描述它可能是用户可以解决问题(例如,找不到文件,磁盘已满,没有到主机的路由等)。

One thing that is helpful, in the absence of a specific field on the exception for the information, is the offending data:

在缺少信息例外的特定字段的情况下,有一件事是有用的:违规数据:

throw new IndexOutOfBoundsException("offset < 0: " + off);

#2


6  

Just be matter of fact. Include all the information you're likely to need when debugging, but no more than that.

只是事实上。包括调试时可能需要的所有信息,但不多于此。

The only time I'd include an exclamation mark in an exception message is if it indicates that something really, really bizarre has happened. Most errors aren't really bizarre, just the product of an incorrect environment, user error, or a simple programming mistake.

我唯一一次在异常消息中包含一个感叹号,如果它表明某些事情确实发生了真正奇怪的事情。大多数错误并不是奇怪的,只是不正确的环境,用户错误或简单的编程错误的产物。

#3


5  

I try to mirror the tone, grammar and punctuation style of the framework against which I'm coding. You never know when one of these messages might actually make it out in front of a client or user, so I keep everything professional, non-judgmental and specific enough for troubleshooting -- without being so specific as to give away any security issues in the code.

我试着反映我正在编码的框架的语气,语法和标点符号样式。你永远不知道其中一条消息何时可能真正在客户或用户面前出现,所以我保持一切专业,非评判和具体足以进行故障排除 - 而不是特别放弃任何安全问题。码。

I avoid exclamation marks in all strings (UI and exception) like the plague, except (ocasionally) in my unit tests.

我在所有字符串(UI和异常)中都避免使用感叹号,例如瘟疫,除了(偶尔)在我的单元测试中。

#4


3  

Taking responsibility, even when it really was the user's fault, is the best option I've seen.

承担责任,即使确实是用户的错,也是我见过的最佳选择。

Things along the lines of "I can't find the file you wanted, would you check to see I have it correctly?" or "Something went wrong. Dunno what, but the only way I can get fixed is by stopping. Please restart me."

根据“我找不到你想要的文件,你会检查我是否正确使用它?”或者“出了点问题。不知道怎么回事,但我能解决的唯一方法就是停止。请重启我。”

#5


2  

Concise, detailed and little redundant information (i.e. ArgumentNullException obviously involved a null).

简明,详细和少量冗余信息(即ArgumentNullException显然涉及null)。

But here's the best i've read for a while, first answer to this.

但这是我读过的最好的一段时间,首先回答这个问题。

#6


2  

I wouldn't use exclamation marks too much. They express too much, think about the fact that "No disk in drive!" can be read as "No disk in drive you crazy user." ;)

我不会太用惊叹号。他们表达太多,想想“驱动器中没有磁盘!”这一事实。可以读作“没有磁盘驱动你疯狂的用户”。 ;)

I think that it's wise to throw exceptions that contain internationalized text. You never know who will use your code, catch your exception and display the text to the user. So that would be:

我认为抛出包含国际化文本的异常是明智的。您永远不知道谁将使用您的代码,捕获您的异常并向用户显示文本。那就是:

throw new MagicalException(getText("magical.exception.text"));

I also recommend wrapping the underlying exception (if you have one) when throwing it. It really helps debugging.

我还建议在抛出它时包装底层异常(如果有的话)。它真的有助于调试。

Don't think that runtime exceptions won't be seen by the user. If you are logging to a file appender some curious user might just open the log and peek into your dirty secrets.

不要以为用户不会看到运行时异常。如果您要登录文件追加器,一些好奇的用户可能只是打开日志并查看您的脏密码。

#7


2  

I find the most helpful messages provide:

我发现最有用的消息提供:

  • A consistent format that makes it easy to understand what they're telling you.
  • 一致的格式,便于理解他们告诉您的内容。

  • A time stamp, so you can get a feel for the dynamics of your program.
  • 时间戳,这样您就可以了解程序的动态。

  • A terse summary of the error. If you provide tech support, add an error code for quick identification.
  • 简要的错误摘要。如果您提供技术支持,请添加错误代码以便快速识别。

  • An explanation of what went wrong, differentiating between an invalid user input and a coding error.
  • 解释出错的地方,区分无效的用户输入和编码错误。

  • Detailed information, including the line of code or values involved.
  • 详细信息,包括涉及的代码行或值。

And most important:

最重要的是:

  • They tell the user how to fix the problem.
  • 他们告诉用户如何解决问题。

Example:

Error 203 (Timeout) in commit.c line 42:
Unable to save salary data for user 'Linus' to database at '10.10.1.21'
after 1500ms.  Verify database address and login credentials.

One of the hardest lessons to learn is that your users are far less interested in the internals of your code than they are in getting their jobs done. Make it as easy as possible for them to do their jobs, and you've added tremendous value to your software.

要学习的最难的课程之一是,您的用户对代码内部的兴趣远远低于完成工作的内容。让他们尽可能轻松地完成工作,并为您的软件增添了巨大的价值。

#8


1  

I tend to work my exception messages into the exception themselves. E.g. a file_not_found should say "file not found". Specific data should only be included if the user can't figure it out; in this case, the user knows the filename, so I don't add that data. Formatting can be done by whatever outputs the information if necessary, so I try to make them as friendly to reformatting as possible.

我倾向于将异常消息自己处理成异常。例如。 file_not_found应该说“找不到文件”。只有在用户无法弄清楚的情况下才应包含特定数据;在这种情况下,用户知道文件名,所以我不添加该数据。如有必要,可以通过任何输出信息来完成格式化,因此我尽量使它们尽可能友好地重新格式化。

#9


1  

Polite, terse, simple, specific. Often, including state values in message is helpful.

礼貌,简洁,简洁,具体。通常,在消息中包含状态值是有帮助的。

#1


4  

A conversational tone in system messages makes the software look unprofessional and sloppy. Exclamation points, insults, and slang don't really have a place in polished exception messages.

系统消息中的对话音使得软件看起来不专业且不马虎。感叹号,侮辱和俚语在抛光的异常消息中确实没有位置。

Also, I tend to use different styles in Java for runtime exceptions and checked exceptions, since runtime exceptions are addressed to the programmer that made the mistake. Since runtime exceptions might be displayed to end users, I still "keep it clean," but they can be a little more terse and cryptic. Checked exception messages should be more helpful, since it may be that the user can fix the problem if you describe it (e.g., file not found, disk full, no route to host, etc.).

此外,我倾向于在Java中使用不同的样式来处理运行时异常和检查异常,因为运行时异常是针对造成错误的程序员的。由于运行时异常可能会显示给最终用户,我仍然“保持清洁”,但它们可能会更加简洁和神秘。检查的异常消息应该更有帮助,因为如果您描述它可能是用户可以解决问题(例如,找不到文件,磁盘已满,没有到主机的路由等)。

One thing that is helpful, in the absence of a specific field on the exception for the information, is the offending data:

在缺少信息例外的特定字段的情况下,有一件事是有用的:违规数据:

throw new IndexOutOfBoundsException("offset < 0: " + off);

#2


6  

Just be matter of fact. Include all the information you're likely to need when debugging, but no more than that.

只是事实上。包括调试时可能需要的所有信息,但不多于此。

The only time I'd include an exclamation mark in an exception message is if it indicates that something really, really bizarre has happened. Most errors aren't really bizarre, just the product of an incorrect environment, user error, or a simple programming mistake.

我唯一一次在异常消息中包含一个感叹号,如果它表明某些事情确实发生了真正奇怪的事情。大多数错误并不是奇怪的,只是不正确的环境,用户错误或简单的编程错误的产物。

#3


5  

I try to mirror the tone, grammar and punctuation style of the framework against which I'm coding. You never know when one of these messages might actually make it out in front of a client or user, so I keep everything professional, non-judgmental and specific enough for troubleshooting -- without being so specific as to give away any security issues in the code.

我试着反映我正在编码的框架的语气,语法和标点符号样式。你永远不知道其中一条消息何时可能真正在客户或用户面前出现,所以我保持一切专业,非评判和具体足以进行故障排除 - 而不是特别放弃任何安全问题。码。

I avoid exclamation marks in all strings (UI and exception) like the plague, except (ocasionally) in my unit tests.

我在所有字符串(UI和异常)中都避免使用感叹号,例如瘟疫,除了(偶尔)在我的单元测试中。

#4


3  

Taking responsibility, even when it really was the user's fault, is the best option I've seen.

承担责任,即使确实是用户的错,也是我见过的最佳选择。

Things along the lines of "I can't find the file you wanted, would you check to see I have it correctly?" or "Something went wrong. Dunno what, but the only way I can get fixed is by stopping. Please restart me."

根据“我找不到你想要的文件,你会检查我是否正确使用它?”或者“出了点问题。不知道怎么回事,但我能解决的唯一方法就是停止。请重启我。”

#5


2  

Concise, detailed and little redundant information (i.e. ArgumentNullException obviously involved a null).

简明,详细和少量冗余信息(即ArgumentNullException显然涉及null)。

But here's the best i've read for a while, first answer to this.

但这是我读过的最好的一段时间,首先回答这个问题。

#6


2  

I wouldn't use exclamation marks too much. They express too much, think about the fact that "No disk in drive!" can be read as "No disk in drive you crazy user." ;)

我不会太用惊叹号。他们表达太多,想想“驱动器中没有磁盘!”这一事实。可以读作“没有磁盘驱动你疯狂的用户”。 ;)

I think that it's wise to throw exceptions that contain internationalized text. You never know who will use your code, catch your exception and display the text to the user. So that would be:

我认为抛出包含国际化文本的异常是明智的。您永远不知道谁将使用您的代码,捕获您的异常并向用户显示文本。那就是:

throw new MagicalException(getText("magical.exception.text"));

I also recommend wrapping the underlying exception (if you have one) when throwing it. It really helps debugging.

我还建议在抛出它时包装底层异常(如果有的话)。它真的有助于调试。

Don't think that runtime exceptions won't be seen by the user. If you are logging to a file appender some curious user might just open the log and peek into your dirty secrets.

不要以为用户不会看到运行时异常。如果您要登录文件追加器,一些好奇的用户可能只是打开日志并查看您的脏密码。

#7


2  

I find the most helpful messages provide:

我发现最有用的消息提供:

  • A consistent format that makes it easy to understand what they're telling you.
  • 一致的格式,便于理解他们告诉您的内容。

  • A time stamp, so you can get a feel for the dynamics of your program.
  • 时间戳,这样您就可以了解程序的动态。

  • A terse summary of the error. If you provide tech support, add an error code for quick identification.
  • 简要的错误摘要。如果您提供技术支持,请添加错误代码以便快速识别。

  • An explanation of what went wrong, differentiating between an invalid user input and a coding error.
  • 解释出错的地方,区分无效的用户输入和编码错误。

  • Detailed information, including the line of code or values involved.
  • 详细信息,包括涉及的代码行或值。

And most important:

最重要的是:

  • They tell the user how to fix the problem.
  • 他们告诉用户如何解决问题。

Example:

Error 203 (Timeout) in commit.c line 42:
Unable to save salary data for user 'Linus' to database at '10.10.1.21'
after 1500ms.  Verify database address and login credentials.

One of the hardest lessons to learn is that your users are far less interested in the internals of your code than they are in getting their jobs done. Make it as easy as possible for them to do their jobs, and you've added tremendous value to your software.

要学习的最难的课程之一是,您的用户对代码内部的兴趣远远低于完成工作的内容。让他们尽可能轻松地完成工作,并为您的软件增添了巨大的价值。

#8


1  

I tend to work my exception messages into the exception themselves. E.g. a file_not_found should say "file not found". Specific data should only be included if the user can't figure it out; in this case, the user knows the filename, so I don't add that data. Formatting can be done by whatever outputs the information if necessary, so I try to make them as friendly to reformatting as possible.

我倾向于将异常消息自己处理成异常。例如。 file_not_found应该说“找不到文件”。只有在用户无法弄清楚的情况下才应包含特定数据;在这种情况下,用户知道文件名,所以我不添加该数据。如有必要,可以通过任何输出信息来完成格式化,因此我尽量使它们尽可能友好地重新格式化。

#9


1  

Polite, terse, simple, specific. Often, including state values in message is helpful.

礼貌,简洁,简洁,具体。通常,在消息中包含状态值是有帮助的。