Exception or not

Monica Leko monica.leko at gmail.com
Mon Mar 3 10:32:33 EST 2008


Suppose you have some HTML forms which you would like to validate.
Every field can have different errors.  For example, this are the
forms:

username
password
etc

And you want to validate them with some class.  Is this good pattern:

class Foo(object):
def validateUsername(username):
  if username isn't correct:
    raise ValidationError()
def validatePassword(password):
  if password isn't correct:
    raise ValidationError()

code:
try:
  usernameError = validateUsername()
except ValidationError:
  usernameError = error from exception
try:
  passwordError = validatePassword()
except ValidationError:
  passwordError = error from exception

So, if there wasn't any errors, usernameError and passwordError both
contains None, and there was error, both contains some string?  Should
I use exception or just return None or some string without
exception?



More information about the Python-list mailing list