解决: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分支或离开tryfinally分支有raise,就会出现这样的提示。

本例中的程序连续发起数千个请求,大量请求失败就会报这个错误:During handling of the above exception, another exception occurred

小伙伴们按下面的解决方法即可解决!!!



解决方法

要解决这个错误,需要可以使用try捕获异常但不用raise,即可解决。

正确的代码是:


...
  try:
    re =  request.get(url)
  exceptprint("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



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

相关推荐

  1. XML 解析异常问题解决

    2023-12-09 04:38:02       31 阅读
  2. 域名解析出错的解决办法

    2023-12-09 04:38:02       26 阅读
  3. Edge问题解决教程

    2023-12-09 04:38:02       59 阅读
  4. 跨域怎么解决

    2023-12-09 04:38:02       60 阅读
  5. php解决XSS攻击

    2023-12-09 04:38:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 04:38:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 04:38:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 04:38:02       82 阅读
  4. Python语言-面向对象

    2023-12-09 04:38:02       91 阅读

热门阅读

  1. 【C语言期末】题目+笔记

    2023-12-09 04:38:02       50 阅读
  2. Vue.component

    2023-12-09 04:38:02       46 阅读
  3. TypeScript中的undefined,void,null

    2023-12-09 04:38:02       57 阅读
  4. C\C++ 获取最值

    2023-12-09 04:38:02       52 阅读
  5. Auth模块的使用

    2023-12-09 04:38:02       49 阅读
  6. 【qml入门系列教程】:qml QtObject用法介绍

    2023-12-09 04:38:02       51 阅读
  7. 【POSTGIS】判定点位是否在范围内

    2023-12-09 04:38:02       50 阅读
  8. 雷军:我的程序人生路

    2023-12-09 04:38:02       51 阅读
  9. leetcode做题笔记1423. 可获得的最大点数

    2023-12-09 04:38:02       54 阅读
  10. mysql 表分区类型

    2023-12-09 04:38:02       58 阅读
  11. 微信小程序保存二维码的过程

    2023-12-09 04:38:02       65 阅读
  12. 策略产品经理常用的ChatGPT通用提示词模板

    2023-12-09 04:38:02       59 阅读
  13. 项目代码规范

    2023-12-09 04:38:02       59 阅读