0

I want to catch exception out of a function, while if finally part exists in the function, the outer part couldn't know exception exists.

Here is the code:

def f():
    l = []

    try:
        raise Exception('fake')
    except:
        raise e
    finally:
        return l

try:
    f()
except:
    print 'wrong'
else:
    print 'right'

Output is

right

And if I remove "finally" part, it will output wrong as expected.

I know the reason from here, but I don't know any elegant way to deal with this scenario, how should I write code if I need re-raise and finally together?

Community
  • 1
  • 1
linpingta
  • 2,324
  • 2
  • 18
  • 36

0 Answers0