I would like to discuss a question about best practices regarding exception handling (e.g. in Java). Normally, when setting the attributes of a class, I check the arguments in the setters for validity, and if the arguments are not valid, I throw an IllegalArgumentException.
Besides the setters, sometimes also the constructor of the class allows to set some values for the attributes. So I also have to check the arguments in the constructor for validity. This, however, would lead to duplicate code. I can only think of one solution for this problem: Instead of directly setting the value of the attribute in the constructor, I call the setter, which then also validates the given value.
However, as the setters are public, this has the problem that I would call an overrideable method from within the constructor, which is bad. To solve this problem again, I could make the setters final, but this I have never seen.
Another possibility I could think of, theoretically, is to have a private or many private methods which check the arguments for validity and which are called by both the constructors and setters. But this would lead to even more code. What do you think are the best practices for this problem?