1

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 ?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
treefrog
  • 1,027
  • 1
  • 15
  • 30

2 Answers2

1

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.

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61
  • I agree they are intended for different things. I don't need to localize. My system is not meant to be extended by other outside parties. Its not a library or anysuch. Its a webapplication. I did not understand your last sentence. – treefrog Jan 04 '11 at 02:21
  • 1
    If you have no need for extensibility, then using an enum for type is a pretty simple and typesafe approach. The last sentence is related to problems with avoid exception type collisions in extensibility scenarios. Your error codes could be strings similar to java package names. – Konstantin Komissarchik Jan 04 '11 at 02:36
  • ah. I need to do something like that anyway because if I use a single file (for enum) then I need to partition the errors anyway. Any suggestions ? I need to maintain the association of exception to errors somehow for readability/maintainability. Any ideas how to ? I added a third option, can you weigh in on that ? – treefrog Jan 04 '11 at 03:02
  • 1
    Just name your enum values appropriately based on groupings. Then use the enum.name() as the key to a properties file (ResourceBundle) holding your error messages. I recommend against proliferation of exception classes in most cases (your option 3). Only useful if you intend to catch and handle some exceptions specially. If all of your handling is generic, one exception class with a type is the way to go. – Konstantin Komissarchik Jan 04 '11 at 03:06
  • There Will be multiple exception classes. They will be handled differently. I was going to put both the code and message into the enum, why do you think I need a resourcebundle in addition to an enum ? – treefrog Jan 04 '11 at 03:27
  • 1
    Typically, strings are externalized into resource files rather than hardcodded in java source, but if you wish to hardcode messages, you can do that in the enum. – Konstantin Komissarchik Jan 04 '11 at 05:08
1

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.

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
  • I should have been clearer- my "program" is an ear, and the error messages are only for those CREATED by our code. not sqlexceptions and the like. I am leaning toward option 2... , what about a third option ? I can have en enum inside every exception class, with code and pattern string for error message. maybe then I can use your suggestons to generate a system wide error message table. not sure how to do it though – treefrog Jan 04 '11 at 02:18