If you are using in Exception for what he should be using, ie exceptions, have no reason to worry about perfomance. The idea is to treat the code as much as possible to avoid errors (testing null objects, for example) and, if something unexpected or not mitigated happens, then fire a Exception.
In the case of your question, you are thinking of the scenario of validation of a business object and firing a Exception if it is not valid. This @Maniero already put well in his answer, if it is a validation rule, it should not fire a Exception, and if so, you should create a custom Exception for each type of validation.
There is no point in firing an Exception and the caller knowing that "something went wrong in the validation"!
Ah, but you have a message, something like "invalid number," but let’s evaluate message? It doesn’t look good, especially since the messages can change... should trigger a custom exception, a class that extends Exception
and shoot, something like:
class ClienteCPFInvalidoExceptionextends Exception { ... }
if (!CPFValido($cliente)) {
throw ClienteCPFInvalidoException();
}
And then use a catch
specific:
catch(ClienteCPFInvalidoException$e){ ... }
But then I would have to create a type for each validation, so that whoever runs the code can "take" the correct Exception and know how to handle, but then, how much code would have to implement?
Maybe it’s more practical in this case of your question, as you yourself mentioned a list of violations and not worry about the cost of a Exception