gpt4 book ai didi

Python 3 处理错误 TypeError : catching classes that do not inherit from BaseException is not allowed

转载 作者:行者123 更新时间:2023-12-04 20:28:14 47 4
gpt4 key购买 nike

当我运行此代码时:

i=0
while i<5:
i=i+1;
try:
SellSta=client.get_order(symbol=Symb,orderId=SellOrderNum,recvWindow=Delay)
except client.get_order as e:
print ("This is an error message!{}".format(i))
#End while
我收到此错误:
TypeError: catching classes that do not inherit from BaseException is not allowed
我读了这个胎面 Exception TypeError warning sometimes shown, sometimes not when using throw method of generator还有这个 Can't catch mocked exception because it doesn't inherit BaseException另请阅读 https://medium.com/python-pandemonium/a-very-picky-except-in-python-d9b994bdf7f0
我用这个代码修复它:
i=0
while i<5:
i=i+1;
try:
SellSta=client.get_order(symbol=Symb,orderId=SellOrderNum,recvWindow=Delay)
except:
print ("This is an error message!{}".format(i))
#End while
结果是忽略错误并转到下一个,但我想捕获错误并打印它。

最佳答案

我发了 question在西类牙堆栈中获得更好的结果。
翻译和总结:
发生该错误是因为在异常子句中您必须指明您捕获的是哪个异常。异常是从基类 Exception 继承(直接或间接)的类。

相反,我将 client.get_order 放在 python 期望异常名称的位置,并且您放置的是对象的方法,而不是从异常继承的类。

解决方案是这样的

try:
SellSta=client.get_order(symbol=Symb,orderId=SellOrderNum,recvWindow=Delay)
except Exception as e:
if e.code==-2013:
print ("Order does not exist.");
elif e.code==-2014:
print ("API-key format invalid.");
#End If

您需要为 here 中的每个异常编码

关于Python 3 处理错误 TypeError : catching classes that do not inherit from BaseException is not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53148112/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com