我无法捕获我发送的正确引发错误(引发 UnicodeDecodeError 并获取 TypeError)

发布于 2025-01-11 18:51:37 字数 1335 浏览 0 评论 0原文

我正在创建一个 Flask 应用程序,每当我尝试从类验证引发 UnicodeDecodeError 并返回到我的主 try 块时,它不会转到 UnicodeDecodeError 异常,而是转到 Exception 块

这是我调用我的类的主要块验证并期待 UnicodeDecodeError

    try:

        validation = Validation(request.files['csvfile'])
        validation.checkextension()
        # columns = validation.check_columns(csv)
        return redirect(request.url)

    except UnicodeDecodeError:
        # return Response("Error : %s"% UnicodeDecodeError)
        return "Please enter a .csv extension"
    except Exception as e:
        print("abc",repr(e))
        # return Response("Error : %s"% Exception)
        return "Exception"

这是我提出错误的类:

class Validation:
def __init__(self,file):
    self.file = file
    self.location = "./uploads/" + str(file.filename)

def checkextension(self):
    try:
        self.file.save(os.path.join("uploads", self.file.filename))
        data = pd.read_csv(self.location)


    except UnicodeDecodeError:
        os.remove("./uploads/" + str(self.file.filename))
        raise UnicodeDecodeError

    except Exception:
        print(Exception)
        raise Exception

另外,当我将 except Exception 中的语句打印为 e 时: 我得到这个输出:

TypeError('function takes exactly 5 arguments (0 given)')

我收到 TypeError 但我正在引发 UnicodeDecodeError

I am creating a flask application and whenever I am trying to raise a UnicodeDecodeError from the class Validation and returning to my main try block then instead of going to UnicodeDecodeError exception it's going to Exception block

This is my main block from where I am calling my class Validation and expecting a UnicodeDecodeError

    try:

        validation = Validation(request.files['csvfile'])
        validation.checkextension()
        # columns = validation.check_columns(csv)
        return redirect(request.url)

    except UnicodeDecodeError:
        # return Response("Error : %s"% UnicodeDecodeError)
        return "Please enter a .csv extension"
    except Exception as e:
        print("abc",repr(e))
        # return Response("Error : %s"% Exception)
        return "Exception"

This is my class from where i am raising the error:

class Validation:
def __init__(self,file):
    self.file = file
    self.location = "./uploads/" + str(file.filename)

def checkextension(self):
    try:
        self.file.save(os.path.join("uploads", self.file.filename))
        data = pd.read_csv(self.location)


    except UnicodeDecodeError:
        os.remove("./uploads/" + str(self.file.filename))
        raise UnicodeDecodeError

    except Exception:
        print(Exception)
        raise Exception

Also When I am printing the statement in except Exception as e:
I am getting this output:

TypeError('function takes exactly 5 arguments (0 given)')

I am getting TypeError but I am raising UnicodeDecodeError

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文