您的位置:首页技术文章
文章详情页

聊聊python中的异常嵌套

【字号: 日期:2022-07-12 16:56:30浏览:2作者:猪猪

在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 zero

The 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中的异常嵌套的详细内容,更多关于python 异常嵌套的资料请关注好吧啦网其它相关文章!

标签: Python 编程
相关文章: