continue statement: best way round a problem
Skip Montanaro
skip at mojam.com
Fri Sep 22 17:47:13 EDT 2000
Steve> I'm trying to write a processing loop which, when any of a number
Steve> of error conditions occurs, proceeds to the next iteration. This
Steve> looked like an obvious case for continue, so my code reads (with
Steve> extraneous elements removed) as follows:
... [snip] ...
Steve> As the advanced reader may have already predicted, I get a syntax
Steve> error with the second continue:
Steve> SyntaxError: 'continue' not properly in loop
I think that's a bug in the Python parser. Clearly, the second continue
statement is in a valid place. It's inside the for loop and not executed
from a try clause. The syntax error seems to be related to the complexity
of the try/except statements.
Here's a workaround:
for i in range(10):
print i
try:
try:
if i > 6:
1/0
except ZeroDivisionError:
raise "continue"
print "a"
try:
if (i+1) % 3 == 0:
print "c"
1/0
except KeyboardInterrupt:
raise
except:
raise "continue"
print "b"
except KeyboardInterrupt:
print "Keyboard Interrupt"
break
except "continue":
continue
If you replace the 'raise "continue"' statements with continue statements
you'll get the syntax error.
--
Skip Montanaro (skip at mojam.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list