I am creating a class that validates strings. There are many reasons why the string might not pass.
Would it make more sense to throw an exception, or return false/error code? Pros/cons?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
validators should not throw exceptions, as failing a validator is not an “exceptional” event.
the rest of your code should throw exceptions if it gets bad data.
when you run a validator function you are clearly prepared to handle any problems detected with a simple test. wrapping everything in a try/catch block and stopping all execution only to try to recover is overkill. Just use and if statement, and be prepared to show the user some error messages.