
MRAB wrote:
Michael wrote:
Here are two examples of why allowing return inside a finally block is a bad idea:
def f(): try: return 3 finally: return 4
def f(): try: raise Exception() finally: return 4
I wouldn't call that a syntax error. Wouldn't you also have to forbid break, continue and, perhaps, yield?
I shouldn't checked before posting... :-( Test 1: break swallows the exception.
for n in range(3): try: print n raise IndexError finally: break
0 Test 2: continue raises a SyntaxError.
for n in range(2): try: print n raise IndexError finally: continue
SyntaxError: 'continue' not supported inside 'finally' clause Test 3: yield propagates the exception on resuming.
def test(): for n in range(3): try: print n raise IndexError finally: print "yielding", n yield n
t = test() t.next() 0 yielding 0 0 t.next()
Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> t.next() File "<pyshell#21>", line 5, in test raise IndexError IndexError