How to avoid hardcoding of exception messages


EJB programming & troubleshooting: How to avoid hardcoding of exception messages

  1. How to avoid hardcoding of exception messages (3 messages)

    This question is a pretty trivial one. But I am looking for a best practice available. In our EJB application, exceptions are caught at different places - in EJB, Facades and in struts action classes. In all the places, we throw application specific exceptions with meaningful messages. Now, my question is, to avoid hardcoding of these exception messages? it good to read it through a properties file or have it as Gloabls public static final variables.

  2. ResourceBundle?[ Go to top ]

    Can you provide more information?

    Are you thinking about localizing the exception messages?

    Have you considered using a ResourceBundle?
  3. One would assume that you want to avoid hard coding because you either:

    a) want to be able to change them without recompiling (killing your Global public static final variable idea), or
    b) want to assign a common message to a large number of exceptions without keying each one in. This would adapt to either of your ideas.

    I would say, store them in an XML file and read them once during startup into some sort of ExceptionFactory. Then dispense at will. This way you can change them without recompilation, though you will have to re-initialize (restart the server(s)) them.
  4. Guys thanks for the comments.
    1) Idea is to reuse message ( localization in long run )
    2) is nt the XML approach a bit complex for a task as this? whats the harm in using a .properties?