[Baypiggies] Discussion for newbies/beginner night talks
Shannon -jj Behrens
jjinux at gmail.com
Wed Feb 14 00:15:52 CET 2007
#) 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()
More information about the Baypiggies
mailing list