异常消息是否以句点结束?

我已经看到了带句点和不带句点的异常消息。我可以想到一些原因,为什么两者都可以是好的。

  • 任何点都不会给你添加句点的自由,也不会给你省略句点的自由。如果信息出现在某种标题栏之类的地方,可能会很有用。
  • 如果有一个点,你总是知道你有一个“完整的句子”,它看起来更完整。

你推荐哪一个?

也可能是本地化资源字符串中的问题。显然,你不能在所有事情之后都加上句号(在按钮和菜单项等文本之后加上句号会显得很奇怪)。但是你是否应该把句点从所有内容中剔除,保持一致,然后在有用的地方加上句点呢?或者你更愿意定一个合适的时间段?例如,在所有属于句子的资源字符串和异常消息之后,而不是那些属于单词的消息之后。但是,如果是非常短的句子呢?比如“创建一个新文件”。也许可以省略那些也被认为是动作的字符串的句号... ... (只是在打字的时候思考... ...)

我知道这不是世界上最重要的事,但这种小事过一阵子就会烦到我。我喜欢一致性,知道我为什么要这么做。问题是,我不知道该选哪个: p

14197 次浏览

Yes I usually treat the exception messages as full sentences, ending them with a period.

However, the message in the exception is meant for the developer, and not the end user. It may very well be that the same underlying exception should result in two different messages for the end user, depending on the context in which the exception-throwing method was called.

You really should show less technical, more user friendly messages to the user.

I always use periods in my exception descriptions. The simple fact is that sentences which are properly punctuated are easier to read and more professional-looking, which is important for perceived quality - don't you think?

Compare it to:

i always use periods in my exception descriptions the simple fact is that sentences which are properly punctuated are easier to read and more professional looking which is important for perceived quality dont you think

Exception messages in the framework are dot-terminated; I tend to do the same for that reason.
In any case, choose a style and try to stick to that...

Use your best judgement. I sometimes use exclamation mark as well. :-)

Exception messages form a part of the developer interface to your application. Interfaces are generally designed with a view to helping the user perform some specific task. In the case of an exception, the interface provided should be designed to convey information about an error which has occurred within the application.

When you decide to throw an exception and write a line like

throw new ArgumentException("The string must contain at least one character.");

then you have already made a number of decisions about the interface including:

  • Exception type
  • Lack of localised exception messages (use of hardcoded string usually implies this)
  • This exception is not the result of any other condition (no inner exception)

Remember, the developer interface exists to serve developers and the user interface to serve the user, the former has vastly different requirements than the latter, so what is good for one might not be good for the other, so a period in the exception message should not concern the user interface because it should not be visible to the end user.

The use of a period is not a major decision in most cases but you should consider whether its presence (or lack thereof) is beneficial or detrimental to the interface by considering points already raised including framework consistency and localisation.

I know this post is a little wordy and possibly a little astronauty, but I hope it is helpful to you.

Q. Do you end your exception messages with a period?

From Best Practices for Exceptions on MSDN in the section "Creating and raising exceptions":

  • Use grammatically correct error messages, including ending punctuation. Each sentence in a description string of an exception should end in a period. For example, "The log table has overflowed.” would be an appropriate description string.

And regarding possible feedback to the user via the the application user interface, the question includes:

...Could also be an issue in localized resource strings.

The MSDN article referenced above also states:

  • Include a localized description string in every exception. The error message that the user sees is derived from the description string of the exception that was thrown, and not from the exception class.

Also, from Exception.Message Property at the beginning of the section "Remarks":

Error messages target the developer who is handling the exception. The text of the Message property should completely describe the error and, when possible, should also explain how to correct the error. Top-level exception handlers may display the message to end-users, so you should ensure that it is grammatically correct and that each sentence of the message ends with a period. Do not use question marks or exclamation points. If your application uses localized exception messages, you should ensure that they are accurately translated.


.NET Framework 4.6 and 4.5