continue statement: best way round a problem

Steve Holden sholden at holdenweb.com
Fri Sep 22 15:59:12 EDT 2000


I'm trying to write a processing loop which, when any of a number
of error conditions occurs, proceeds to the next iteration.  This
looked like an obvious case for continue, so my code reads (with
extraneous elements removed) as follows:

#
# Iterate over database rows, processing each one in turn
#
donect = 0
for b in billings:
    try:
        # Some complicated stuff
        try:
            rtfstream = open("%(year)d/%(monthno)d/%(account)s" % bd).read()
        except:
            lf.log("??? Cannot find invoice file for %s" % bd["account"])
            #
            # No invoice file means we should go on to next client
            #
            continue   ######### no syntax error here
        # some more complicated stuff
        try:
            mxlist = MXlist(domain(bd["recipient"]), "ns2.i95.net")
        except KeyboardInterrupt:
            raise
        except:
            lf.log("an error message")
            lf.debug("a debugging message")
            #
            # No name servers means we should go on to the next client
            #
            continue   ######### syntax error
        lf.log("an informative message")))
        # A bit more complicated stuff
    except KeyboardInterrupt:
        print "Keyboard Interrupt"
        lf.log("??? Terminated by keyboard interrupt")
        break

As the advanced reader may have already predicted, I get a syntax error
with the second continue:

SyntaxError: 'continue' not properly in loop (line [the second continue])

But I'm left trying to understand why the compiler isn't flagging an
error for the first continue when it won't accept the second.

In general, what's the accepted way of handling such cases?

regards
 Steve

PS The documentation is explicit that "continue" is valid inside an
   "except", but not in a "try" (due, as a footnote points out, to
   the implementor's laziness).  Is this the problem?
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/




More information about the Python-list mailing list