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

Should I create a closed static final String = "Some exception message" or leave it inside the code? Are there any performance issues? I have a lot of exceptions. The texts in most cases are different. Caution about performance and memory issues. Internationalization is possible in the future.

+4
source share
3 answers

The exception to the Bojo and Thilo answers, IMO, is when you use the exception message in several places. If you are going to move a single message to a static end line, do this for all exceptions in this file for consistency. If you find one exception message at the top of the file, the logic assumes that they can all be found there.

+2
source

No difference in terms of performance.

So, for the sake of readability, and if you are not using the same line in another place, leave it inside the code.

As for internationalization - I am against the idea of ​​i18n exceptions, but if you do:

  • throw an exception using the message key (from a class with static final ).
  • allow the corresponding message only when it is necessary to display an exception.
+6
source

If you use it in only one place and do not plan to provide internationalization, leave it built-in.

If you use it in more than one place, especially if the message becomes part of your interface (so you can check the message when you catch the exception, although you really have to use a separate error code or subclasses for this), make it permanent (if necessary public )

If you need to localize the message, enter it in the message resource file.

+2
source

Source: https://habr.com/ru/post/1304268/


All Articles