try 和 except 都引发异常。
Exception raised in both try and except .
def FancyDivide(list_of_numbers, index):
try:
try:
raise Exception("0")
finally:
denom = list_of_numbers[index]
for i in range(len(list_of_numbers)):
list_of_numbers[i] /= denom
except Exception, e:
print e
调用函数时,我得到以下输出。
FancyDivide([0, 2, 4], 0)
integer division or modulo by zero
在 try 代码中引发了异常。 finally中也有异常,为什么是finally中的异常被捕获,而不是try中的异常。
A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed.
(强调我的)
如前所述,异常 - Exception("0")
只会在完成 finally
块后再次引发。但是因为 finally
块中发生了异常,所以它被引发而不是 Exception("0")
.
def FancyDivide(list_of_numbers, index):
try:
try:
raise Exception("0")
finally:
denom = list_of_numbers[index]
for i in range(len(list_of_numbers)):
list_of_numbers[i] /= denom
except Exception, e:
print e
调用函数时,我得到以下输出。
FancyDivide([0, 2, 4], 0)
integer division or modulo by zero
在 try 代码中引发了异常。 finally中也有异常,为什么是finally中的异常被捕获,而不是try中的异常。
A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed.
(强调我的)
如前所述,异常 - Exception("0")
只会在完成 finally
块后再次引发。但是因为 finally
块中发生了异常,所以它被引发而不是 Exception("0")
.