Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I create private static final String = "Some exception message" or leave it inside the code?

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.

like image 346
Artsiom Anisimau Avatar asked Dec 29 '22 23:12

Artsiom Anisimau


2 Answers

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.
like image 104
Bozho Avatar answered Dec 31 '22 11:12

Bozho


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.

like image 39
Chris Avatar answered Dec 31 '22 13:12

Chris