如果“ try”子句引发SyntaxError,则Python“ finally”子句不会执行

时间:2018-12-10 13:03:11

标签: python exception finally try-finally

考虑以下代码:

try:
    a = []
    a[4] = 4
except ZeroDivisionError:
    print('h')
finally:
    print('y')

输出显示finally子句已执行:

y
Traceback (most recent call last):
  File "work/finally.py", line 3, in <module>
    a[4] = 4
IndexError: list assignment index out of range

但是,如果我将try子句更改为这样的话:

[] += [] - []

然后finally子句不执行:

  File "work/finally.py", line 2
    [] += [] - []
    ^
SyntaxError: illegal expression for augmented assignment

此行为的原因是什么?我在哪里可以读到?我没有在文档中找到它。

0 个答案:

没有答案