python中的异常输出

时间:2018-09-02 06:02:46

标签: python

def fancy_divide(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 as ex:
        print(ex)

调用fancy_divide([0,2,4],0)时,此代码是否打印0?

1 个答案:

答案 0 :(得分:1)

它不会打印0,因为finally块中引发了另一个异常,从而替换了原始异常Exception("0"),因此外部try-except块仅打印了后者