Why is `continue` not allowed in a `finally` clause in Python?

The use of continue in a finally-clause is forbidden because its interpretation would have been problematic. What would you do if the finally-clause were being executed because of an exception?

for i in range(10):
    print i
    try:
       raise RuntimeError
    finally:
       continue        # if the loop continues, what would happen to the exception?
    print i

It is possible for us to make a decision about what this code should do, perhaps swallowing the exception; but good language design suggests otherwise. If the code confuses readers or if there is a clearer way to express the intended logic (perhaps with try: ... except Exception: pass; continue), then there is some advantage to leaving this as a SyntaxError.

Interestingly, you can put a return inside a finally-clause and it will swallow all exceptions including KeyboardInterrupt, SystemExit, and MemoryError. That probably isn't a good idea either ;-)


The Python Language Reference forbids the use of continue within a finally clause. I'm not entirely sure why. Perhaps because continue within the try clause ensures that the finally is executed, and deciding what continue should do within the finally clause is somewhat ambiguous.

Edit: @Mike Christensen's comment to the question points out a thread where this construction's ambiguity is discussed by Python core developers. Additionally, in more than nine years of Python use, I've never wanted to do this, so it's probably a relatively uncommon situation that developers don't feel like spending much time on.