聊聊python中的异常嵌套

聊聊python中的异常嵌套,第1张

聊聊python中的异常嵌套

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

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

运行程序:

Traceback (most recent call last):
File "", line 3, in
raise IndexError
IndexError


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

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

运行:

finally
ok

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


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

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

运行:

Traceback (most recent call last):
File "", line 2, in
1/0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "", line 4, in
raise TypeError('bad') from E
TypeError: bad

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

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

运行:

no


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

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

运行:

finally
ok

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

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

运行:

happens
finally

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

以上就是聊聊python中的异常嵌套的详细内容,更多关于python 异常嵌套的资料请关注考高分网其它相关文章!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3209982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-03
下一篇 2022-10-03

发表评论

登录后才能评论

评论列表(0条)

保存