Python 中异常嵌套,,在Python中,异


在Python中,异常也可以嵌套,当内层代码出现异常时,指定异常类型与实际类型不符时,则向外传,如果与外面的指定类型符合,则异常被处理,直至最外层,运用默认处理方法进行处理,即停止程序,并抛出异常信息。如下代码:

try:    try:        raise IndexError    except TypeError:        print(‘get handled‘)except SyntaxError:    print(‘ok‘)

运行程序:

Traceback (most recent call last):  File "<pyshell#47>", line 3, in <module>    raise IndexErrorIndexError

再看另一个被外层try-except捕获的例子:

try:    try:        1/0    finally:        print(‘finally‘)except:    print(‘ok‘)

运行:

finallyok

这里值得注意的是except:可以捕获所有的异常,但实际上这样做也有缺点,即有时候会包住预定的异常。

另外,需要提到的是raise A from B,将一个异常与另一个异常关联起来,如果from后面的B没有被外层捕获,那么A,B异常都将抛出,例如:

try:    1/0except Exception as E:    raise TypeError(‘bad‘) from E

运行:

Traceback (most recent call last):  File "<pyshell#4>", line 2, in <module>    1/0ZeroDivisionError: division by zeroThe above exception was the direct cause of the following exception:Traceback (most recent call last):  File "<pyshell#4>", line 4, in <module>    raise TypeError(‘bad‘) from ETypeError: bad

相反,如果外层捕获了B:

try:    try:        1/0    except Exception as E:        raise TypeError from Eexcept TypeError:    print(‘no‘

运行:

no

最后,再看看try-finally在嵌套中的表现。

 try:    try:        1/0    finally:        print(‘finally‘)except:    print(‘ok‘)

运行:

finallyok

不管有没有异常发生,或者其是否被处理,finally的代码都要执行,如果异常被处理,则停止,如果没有被处理,向外走,直至最终没处理,采用默认方法处理,上例中,异常在最外层被处理。

try:    try:        1/0    except Exception as E:        print(‘happens‘)    finally:        print(‘finally‘)except E:    print(‘get handled‘)

运行:

happensfinally

异常在内部被处理,不再向外传播。

Python 中异常嵌套

评论关闭