Python

python 重新抛出上一个异常

2022-06-14  本文已影响0人  孙广宁

14.6 我们在except块中捕获一个异常,现在将它重新抛出

>>> def e():
...     try:
...         int('N/A')
...     except ValueError:
...         print("didn`t work")
...         raise
... 
>>> e()
didn`t work
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in e
ValueError: invalid literal for int() with base 10: 'N/A'
>>> 

>>> def e():
...     try:
...         int('N/A')
...     except Exception as e:
...         # 做一些记录日志等的处理,然后继续抛出异常
...         raise
... 
>>> e()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in e
ValueError: invalid literal for int() with base 10: 'N/A'
>>> 

上一篇下一篇

猜你喜欢

热点阅读