解決: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 = 0try:result = x / y
except ZeroDivisionError:print("處理第一個except")try:result = x / yexcept Exception as e:print("處理第二個except")print(repr(e))
當報錯發生在finally
時,需要在finally
的代碼塊里增加異常的try...except...
,再次進行異常處理。可以參考如下代碼:
x = 1
y = 0try:result = x / y
except ZeroDivisionError:print("處理第一個except")finally:try:result = x / yexcept Exception as e:print("處理第二個except")print(repr(e))
參考內容:
https://blog.csdn.net/weixin_39514626/article/details/111839821