Should I create private static final String = "Some exception message" or leave it inside the code? Is there any performance issues? I have a lot of exception cases. Texts are mostly different in any particular case. Wary about performance and memory issues. Internationalization is quite possible in future recs.
views:
187answers:
3Should I create private static final String = "Some exception message" or leave it inside the code?
No difference from performance point of view.
So, for the sake of readability, and in case you don't use the same string elsewhere - leave it inside the code.
As for internationalization - I'm opposed to the idea of i18n exceptions, but if you do so:
- construct the exception with a message key (from a class with
static final
message keys). - resolve the corresponding message only when the exception needs to be displayed.
If you use it in only one place, and do not plan to provide internationalization, leave it inline.
If you use it in more than one place, especially if the message becomes part of your interface (so that you can check against the message when you catch the exception, although you should really use a separate error code or subclasses for that), make it a constant (a public one if necessary).
If you need to localize the message, externalize it into a message resource file.
An exception to the answers from Bozho and Thilo, IMO, is when you ARE using an exception message in more than one place. If you're going to move one message to a static final string, do so for all exceptions within that file for consistency. If one finds one exception message at the top of a file, logic would suggest that all of them might be found there.