在全局位置存储异常消息

发布于 2024-10-09 21:25:47 字数 249 浏览 7 评论 0原文

我希望有一个存储异常消息的位置(这些消息不是面向用户的)。 我也有一些例外,它们可能有不同的错误消息和代码。这些代码仅用于文档和通信目的。但是,将所有错误消息放在一个地方对于引用错误并为

我正在考虑的操作人员提供建议的修复非常有价值:

  1. 所有异常消息的资源包
  2. 包含消息和代码
  3. 全局枚举,每个枚举在每个异常类中都 枚举,每个都包含异常可以具有的消息和代码。

哪个是最好的选择?

I would like to have a single location where exception messages are stored (these are not user facing).
I also have some exceptions which can have different error messages and codes. These codes are intended only for documentation and communication purposes. However, having all the error messages in one place is very valuable to refer to errors and provide suggested fixes for the operations people

I am considering these:

  1. Resourcebundle for all exception messages
  2. global enum with each enum containing a message and code
  3. enum inside every Exception class, with each with message and code that exception can have.

Which is the best option ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知在何时 2024-10-16 21:25:47

资源包和枚举不能解决问题的同一部分。您需要用于本地化的文本外部化(即资源包或类似的)和某种识别消息类型的方法。如果您的系统不可扩展(您可以控制所有异常类型),那么枚举是跟踪消息类型的好方法。如果其他人可以扩展您的系统,则枚举不会成为一个好的类型,因为他们无法添加新代码。我建议使用异常类型的字符串,因为在必要时这很容易适合命名空间分区。

A resourcebundle and an enum aren't solving the same part of the problem. You need both text externalization for localization (that's resourcebundle or similar) and some way to identify message type. An enum is a decent way to track message type if your system isn't extensible (you are in control of all exception types). An enum would not make a good type if others can extend your system as they would have no way to add new codes. I recommend using strings to exception type as that easily lends itself to namespace partitioning, when necessary.

染柒℉ 2024-10-16 21:25:47

我不建议这样做。如果您想让操作员阅读一些内容,请使用某种形式的 Javadoc 或 doclet 创建一个包含系统中所有异常消息的文档。您甚至可以使用 Doxygen

通过创建包含每个异常消息/代码的单个文件,您可以将所有代码绑定在一起,通过该单个文件创建一个整体程序。您的系统中必然存在不需要包含 SQL 异常的部分,或者不应包含 SQL 异常的用户界面异常。

I can't suggest doing either of these. If you want to give operators something to read then use some form of Javadoc or doclet to create a document containing all the Exception Messages in your system. You could even use Doxygen.

By creating a single file with every single exception message/code you have tied all your code together creating a monolithic program through that single file. There will bound to be parts of your system that do not need to include SQL exceptions, or user interface exceptions that shouldn't have access to SQL Exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文