Proper way in Python to raise errors while setting variables

Posted by ensnare on Stack Overflow See other posts from Stack Overflow or by ensnare
Published on 2010-03-26T18:45:02Z Indexed on 2010/03/26 19:13 UTC
Read the original article Hit count: 487

Filed under:
|
|
|

What is the proper way to do error-checking in a class? Raising exceptions? Setting an instance variable dictionary "errors" that contains all the errors and returning it?

Is it bad to print errors from a class? Do I have to return False if I'm raising an exception?

Just want to make sure that I'm doing things right. Below is some sample code:

@property
def password(self):
    return self._password

@password.setter
def password(self,password):
    # Check that password has been completed
    try:
        # Check that password has a length of 6 characters
        if (len(password) < 6):
            raise NameError('Your password must be greater \
                             than 6 characters')

    except NameError:
        print 'Please choose a password'
        return False

    except TypeError:
        print 'Please choose a password'
        return False                                                                                                                                

    #Set the password
    self._password = password

    #Encrypt the password
    password_md5 = md5.new()
    password_md5.update(password)
    self._password_md5 = password_md5.hexdigest()

© Stack Overflow or respective owner

Related posts about python

Related posts about pylons