Does 'finally' always execute in Python?

前端 未结 6 2127
眼角桃花
眼角桃花 2021-01-29 21:38

For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed?

For example, let’s say I return while in an <

6条回答
  •  我在风中等你
    2021-01-29 22:31

    Addendum to the accepted answer, just to help to see how it works, with a few examples:

    • This:

       try:
           1
       except:
           print 'except'
       finally:
           print 'finally'
      

      will output

      finally

    •    try:
             1/0
         except:
             print 'except'
         finally:
             print 'finally'
      

      will output

      except
      finally

提交回复
热议问题