[Tutor] try except continue
alan.gauld at freenet.co.uk
Thu Jul 28 23:25:15 CEST 2005
> program, when it runs through its steps and encounters an error, to
> the error and pick up where it left off and keep going. According
> to this
> link, 'continue' is allowed within an except or finally:
Thats true but only if the try block is inside a loop.
error = True
if error: raise ValueError
else: print 'In f()'
print 'In g()'
error = False
continue will cause the *next* iteration of the loop to start. Thus
the first time round the error is raised in f() and the code jumps
to the except clause and from there back to the top of the loop,
effectively missing g() out, then next time through no error is
raised so both f() and g() are called.
If you really want to ignore the error and move to the next line you
have to do a try:except on every line (or function call)
Or put the functions in a list if their parameter lists are null
funcs = [f,g]
for func in funcs:
But all of that's bad practice since who knows what nasties you
might be allowing through. Its usually possible to structure
code to avoid such horrors!
Author of the Learn to Program web tutor
More information about the Tutor