I am reading a lot about patterns and code structure and something that bothers me is BeanValidation’s way to handle errors. I like Java and think that BeanValidation is easy to use, but it seems to me that using BeanValidation is kind of an anti pattern.
In one project I have just the normal BeanValidation which I catch with an ExceptionMapper to customize the response in my resource. Here is BeanValidation in use which in turn throws exceptions when errors occur.
In another project — which contains lots of legacy code — the structure is similar. The resource injects the ValidationService class which has some validation methods. In that ValidationService the checks BeanValidation is trigged manually (don’t ask why, it’s old code) and then some additional checks are done manually. When an error occurs it is gathered until the end of the validation method and then thrown.
I should also mention that all cases in the ValidationService are defined by domain experts.
Both scenarios end in the ExceptionMapper which generates a response for the client. Both use Exceptions for that.
Now when reading all of the exceptions for control flow are anti pattern articles(1), I am wondering if it is the case here as well. And if so, is there a better solution to that?
Since Java is not able to return a second value in methods like in Go, there’s no good way to return an error otherwise, isn’t it?
(1) like this one: https://softwareengineering.stackexchange.com/a/189225/354474