The analysis is never so simple, everything depends on context, but in general this is it, should throw an exception preventing the object to be built. Not being able to create an object is usually an exceptional case, the normal would be to create the object without problems. Note that the exception in general is a programming error. A test should make it be launched and you should fix.
In some cases it may be that create an invalid situation with null is interesting, but it is not so common and only casting the exception can do it in the builder (a Simple Factory can return the null). You can see that several objects when they cannot be created are null and do not generate an exception, so it is a solution that makes sense. Just don’t push this. This solution is better when the language helps avoid nulls, which is not the case with Java, which has not prevented the language from adopting the solution several times.
Make it clear that an invalid object should not be built. How to prevent this can vary a little. A null is an invalid object, but not an invalid object.
I’m not saying I can’t do that either.
Particularly I would use validation as a last resort when everything failed before, I would try not to let a wrong argument be passed. Popping validation during object creation is exceptional because the error was calling the constructor invalid.
I’ve seen some cases and I’ve thought about making more and more a static method utility that does a validation and only after going through it is that the creation of the object is performed, so you already know that everything will work. You can only not validate in the constructor because nothing requires that this utility be called before the constructor, and it would be even complicated to maintain state indicating that it was validated, it would just be a facility.
I know that the philosophy of the Java community is to abuse exceptions (or at least it was, it has changed she has preferred to use Factories and return a Optional
). So just leave the exception is not wrong, just not good.
I would prefer it had a different mechanism to indicate programming error, which I think is the case, but as there is, the exception is interesting Now you have.
You just won’t put one try-catch
to try to solve when creating this object. Not that there is no situation that cannot be useful, but it is rare. Usually you have to stop the wrong creation before.
Anything can when it makes sense. That’s why I always say that following recipes doesn’t work, you have to understand why you’re doing it.
Take a look at How and when to build a valid state object?.
Validate this before going to the constructor, and tell the user q is invalid, allowing it to report again. Throwing exception is not a solution to anything, alias, is a bad practice. Exceptions is to be used in "exceptions", as the name says.
– user28595
@Shall I then validate it as a method independent of the class I intend to instantiate? It would be interesting to have a class only for the?
– Lucas Dirani
I don’t know, it depends. If it’s too many validations, it would be a case.
– user28595