Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 892
Somebody still uses you MS-DOS Avatar asked Sep 08 '10 20:09

Somebody still uses you MS-DOS


People also ask

Should validation methods throw exception?

Notice how none of the validation methods throw exceptions. Rather they return true or false. There is no need to throw an exception even if the input data is invalid. In contrast, inside a DAO class you will most likely not be able to interact with the user to correct the error.

What is the difference between exception and validation?

Data validation means checking the data before performing operations that could fail, for example check for 0 before doing a division. Exception handling means well defined behavior if an operation fail, for example if a database query times out.

Under what circumstances should you implement exception handling?

Use exception handling if the event doesn't occur often, that is, if the event is truly exceptional and indicates an error, such as an unexpected end-of-file.


2 Answers

This is a very old question but since the only answer - IMO - is not applicable to Python, here comes my take on it.


Exceptions in Python is something many programmers new to the language have difficulties dealing with. Compared to other languages, Python differs significantly in how exceptions are used: in fact Python routinely uses exceptions for flow control.

The canonical example is the for loop: you will certainly agree that there is nothing "uniquely bizarre" about the loop exhausting its iterations (indeed that's what all loops do, unless broken)... yet rather than checking in advance if there are still values to process, Python keeps on trying reading values from the iterable, and failing that, rises the StopIterator exception, which in turn is catch by the for expression and make the code exiting the loop.

Furthermore, it is idiomatic in Python to go by the EAFP (it's Easier to Ask for Forgiveness than Permission = try-except) rather than LBYL (Look Before You Leap = if not A, B or C then).

In this regard, csj's answer is correct for C or Java but is irrelevant for Python (whose exceptions are seldom "exceptional" in nature).

Another factor to consider - though - is the scenario in which user data is invalid but you fail to act on the validation function outcome:

  • with a return statement, failing to process the False value will result in having your non-valid data sent down the pipeline,
  • contrarily, if you were to raise an Exception, failing to catch it would result in the exception propagating through your stack eventually resulting in your code to halt.

While the second option might seems scary at first, it is still the right road to take: if data is invalid, there is no sense in passing it further down the line... it will most probably introduce difficult-to-track bugs later on in the flow and you will have also missed the chance to fix a bug in your code (failing to act on non-valid data).

Again. Using exceptions is the pythonic way to do (but it does not apply to most other languages) as also stated in this other answer and in the zen of python:

Errors should never pass silently.

Unless explicitly silenced.

HTH!

like image 63
mac Avatar answered Sep 29 '22 12:09

mac


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.

like image 33
csj Avatar answered Sep 29 '22 11:09

csj