Search code examples
javadesign-patternsexceptionmaintainability

Storing exception messages in a global location


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 ?


Solution

  • 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.