Search code examples
pythonvalidationexceptionstatus

Is it better to use exceptions in a "validation" class or return status codes?


Suppose I'm creating a class to validate a number, like "Social Security" in US (just as an example of a country-based id). There are some rules to validate this number that comes from an input in a html form in a website.

I thinking about creating a simple class in Python, and a public validate method. This validate returns True or False, simply. This method will call other small private methods (like for the first 'x' numbers if there is a different rule), each one returning True or False as well.

Since this is really simple, I'm thinking of using boolean status codes only (if it's valid or not, don't need meaningful messages about what is wrong).

I've been reading some articles about using exceptions, and I would like to know your opinion in my situation: would using exceptions would be a good idea?


Solution

  • If an input is either valid or not, then just return the boolean. There's nothing exceptional about a validation test encountering an invalid value.