Chu*_*uox 11 python exception typeerror binance
当我运行此代码时:
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
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TypeError: catching classes that do not inherit from BaseException is not allowed
Run Code Online (Sandbox Code Playgroud)
我读了这个有时会显示的胎面Exception TypeError 警告,有时在使用生成器的 throw 方法时不会,而这个无法捕获模拟异常,因为它不继承 BaseException也读了这个https://medium.com/python-pandemonium/a -非常挑剔的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
Run Code Online (Sandbox Code Playgroud)
结果是忽略错误并转到下一个,但我想捕获错误并打印它。
Chu*_*uox 18
我在西班牙堆栈中发布了这个问题,结果更好。翻译和总结:发生错误是因为在异常子句中您必须指出您捕获的异常。异常是从基类 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
Run Code Online (Sandbox Code Playgroud)
您需要为此处的每个异常编码