[Baypiggies] Discussion for newbies/beginner night talks

Shannon -jj Behrens jjinux at gmail.com
Wed Feb 14 22:19:36 CET 2007


On 2/13/07, Aahz <aahz at pythoncraft.com> wrote:
> On Tue, Feb 13, 2007, Shannon -jj Behrens wrote:
> >
> > #) Using try/except for flow control:
> >
> > """Try to actually log the user in.
> >
> > BASIC IDEA: A million things can go wrong, and there are several ways to
> > succeed.  You want to check for the ways that can succeed and continue on as
> > soon as one of them does.  You want the non-local flow of control that
> > exceptions provide, but you need it for both success *and* failure.
> >
> > """
> >
> > def doLoginAction(self):
> >     """Try to actually log the user in."""
> >     class PasswordAccepted(Exception): pass
> >     try:
> >         if check_password():        # This may raise KeyError.
> >             raise PasswordAccepted
> >         do_more_expensive_work()
> >         and_even_more_expensive_work()
> >         if check_other_password():  #  This may raise KeyError.
> >             raise PasswordAccepted
> >         raise KeyError
> >     except KeyError:
> >         self.setError("Invalid username or password.")
> >         return
> >     except PasswordAccepted:
> >         pass
> >     continue_successfully()
>
> This really should not call setError(), it should simply raise
> ValueError (ValueError is for invalid data).

Sorry for the confusion.  setError() is fictitious.  Presumably, you
would do something like create an error response for the user with the
given warning.  Perhaps setError could be replaced with code that
redirects the user to an error page.

-jj

-- 
http://jjinux.blogspot.com/


More information about the Baypiggies mailing list