解决:During handling of the above exception, another exception occurred
文章目录
- 解决:During handling of the above exception, another exception occurred
- 背景
- 报错问题
- 报错翻译
- 报错位置代码
- 报错原因
- 解决方法
- 参考内容:
- 今天的分享就到此结束了
背景
在使用之前的代码时,报错:
Traceback (most recent call last):
File “xxx”, line xx, in
re = request.get(url)
During handling of the above exception, another exception occurred
报错问题
Traceback (most recent call last):
File "xxx", line xx, in
re = request.get(url)
During handling of the above exception, another exception occurred
报错翻译
主要报错信息内容翻译如下所示:
Traceback (most recent call last):
File "xxx", line xx, in
re = request.get(url)
During handling of the above exception, another exception occurred
翻译:
追溯(最近一次通话):
文件“xxx”,第xx行,在
re=request.get(url)
在处理上述异常的过程中,发生了另一个异常
报错位置代码
...
re = request.get(url)
...
报错原因
经过查阅资料,发现是这个错误通常是由于在处理异常的except
分支或离开try
的finally
分支有raise
,就会出现这样的提示。
本例中的程序连续发起数千个请求,大量请求失败就会报这个错误:During handling of the above exception, another exception occurred
小伙伴们按下面的解决方法即可解决!!!
解决方法
要解决这个错误,需要可以使用try
捕获异常但不用raise
,即可解决。
正确的代码是:
...
try:
re = request.get(url)
except:
print("time out")
...
当报错发生在except
时,需要在except
的代码块里增加异常的try...except...
,再次进行异常处理。可以参考如下代码:
x = 1
y = 0
try:
result = x / y
except ZeroDivisionError:
print("处理第一个except")
try:
result = x / y
except Exception as e:
print("处理第二个except")
print(repr(e))
当报错发生在finally
时,需要在finally
的代码块里增加异常的try...except...
,再次进行异常处理。可以参考如下代码:
x = 1
y = 0
try:
result = x / y
except ZeroDivisionError:
print("处理第一个except")
finally:
try:
result = x / y
except Exception as e:
print("处理第二个except")
print(repr(e))
参考内容:
https://blog.csdn.net/weixin_39514626/article/details/111839821