Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The following code raises a syntax error:

>>> for i in range(10):
...     print i
...     try:
...        pass
...     finally:
...        continue
...     print i
...
  File "<stdin>", line 6
SyntaxError: 'continue' not supported inside 'finally' clause

Why isn't a continue statement allowed inside a finally clause?

P.S. This other code on the other hand has no issues:

>>> for i in range(10):
...     print i
...     try:
...        pass
...     finally:
...        break
...
0

If it matters, I'm using Python 2.6.6.

like image 934
ElenaT Avatar asked Nov 28 '11 21:11

ElenaT


People also ask

Does finally execute after continue Python?

From the python docs : 'on the way out' basically means, if a continue statement is executed inside of an exception clause, the code in the finally clause will be executed and then the loop will continue on to the next iteration.

Does finally execute after continue?

When a continue statement occurs within a finally block, the target of the continue statement must be within the same finally block. You cannot continue execution of the loop, because an uncaught exception will transfer control to another function, otherwise, a compile-time error occurs.

What does the finally clause do in Python?

Definition and Usage It defines a block of code to run when the try... except...else block is final. The finally block will be executed no matter if the try block raises an error or not. This can be useful to close objects and clean up resources.

Can we return in finally block Python?

You'll notice that python always returns the last thing to be returned, regardless that the code "reached" return 1 in both functions. A finally block is always run, so the last thing to be returned in the function is whatever is returned in the finally block.


3 Answers

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 ;-)

like image 171
Raymond Hettinger Avatar answered Sep 19 '22 18:09

Raymond Hettinger


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.

like image 24
Michael Hoffman Avatar answered Sep 21 '22 18:09

Michael Hoffman


A continue statement was illegal in the finally clause due to a problem with the implementation. In Python 3.8 this restriction was lifted.

The bug was issue32489 - Allow 'continue' in 'finally' clause.

The pull request for the fix: https://github.com/python/cpython/pull/5822

like image 31
wim Avatar answered Sep 21 '22 18:09

wim