That's a lot of extra work though for questionable gain (maybe even loss). If the initial parameters are invalid, just return null and have the calling code decide what to do.Never throw exceptions unless the conditions are exceptional. Please explain what code you saw or what conversation you had that lead you to see an alternative to an [email protected], validating the parameters ahead of time will end up violating DRY. What specific suggestion caused you to ask this question? I'm not sure his "specific case" is specific enough to give such specific advice. (Don't Repeat Yourself) As you'll need to copy that pre-validation to any code that tries to instantiate this class.Anyway, the factory is a good idea here, and TBH it should throw an exception instead of returning a null.Assuming you create only few 'bad' classes, then making the factory throw is preferable. If My Class was actually a data repository class, and parameter text was a connection string, then best practice would be to throw an Argument Exception.
To avoid having to try/catch this error, then you could use a factory pattern to handle the object creation for you, which would include an empty string case.
I'm thinking that in this case, an empty value can be passed easily.
If that is the case, using this pattern would avoid exceptions and the performance penalties they incur while keeping the My Class state valid. Why is testing the result of a factory for null preferable to having a try-catch somewhere that can catch the exception from the constructor?
I'd want to see some justification for why you think the general rule does not apply never did accept that exceptions were superior to return codes, they can be a humungous performance hit if thrown too often.
However, throwing an exception from a constructor will leak memory, even in .