return from finally clause

Jürgen Hermann jh at web.de
Fri Jun 8 15:28:46 EDT 2001


Consider this script:

def spam(ret):
    try:
        print 1
        try:
            print 2
            raise RuntimeError
        finally:
            print 3
            if ret: return
    finally:
        print 4
    print 5

spam(1)
spam(0)

and its output

1
2
3
4
1
2
3
4
Traceback (most recent call last):
  File "exp.py", line 15, in ?
    spam(0)
  File "exp.py", line 6, in spam
    raise RuntimeError
RuntimeError

That means that if you (accidently?) return within a finally clause, you
effectively eat the exception. This should be documented, or fixed.





More information about the Python-list mailing list