Question

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 ?

Was it helpful?

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top