我使用一个云平台来运行一个程序,当它遇到一个在try/except块中的错误时,我的代码就会崩溃。我不知道这是否是由于平台,但我需要一个方法,以避免程序崩溃。
try:
r = self.http.request('GET', 'https://www.alphavantage.co/query?function=TIME_SERIES_INTADAY&symbol=VIX&interval=1min&apikey=apikey')
data = json.loads(r.data)
if 'Time Series (1min)' in data.keys():
self.VIX = Decimal(data['Time Series (1min)'][list(data['Time Series (1min)'].keys())[0]]['4. close'])
else:
raise Exception("key")
except Exception as e:
self.Debug('VIX Error: ' + str(e))
try:
r = self.http.request('GET', 'https://www.google.com/finance/getprices?q=VIX&i=60&p=1d&f=c') #f=d,o,h,l,c,v'
s = (r.data).decode('utf-8')
l = list(s.splitlines())
self.VIX = Decimal(l[-1])
except Exception as e:
self.Debug('VIX Error: ' + str(e)) #change after last deployment
if (type(self.VIX) is Decimal) == False:
self.VIX = 0LiveTradingRealTimeHandler.Run():调度事件QuantConnect.Scheduling.ScheduledEvent中有一个错误。错误是UnicodeDecodeError:'utf-8‘编解码器无法解码位置为57360的字节0xa0 :无效的开始字节 运行时错误: UnicodeDecodeError:'utf-8‘编解码器无法解码位置为57405的字节0xa0 : main.py:line中的OnData上的无效开始字节,417在GetVix中的main.py:line 458 UnicodeDecodeError:'utf-8’编解码器无法在57405位置解码字节0xa0 : UnicodeDecodeError : System.Exception: UnicodeDecodeError:'utf-8‘编解码器无法在57405位置解码字节0xa0 :在main.py:line 458 -中main.py:line 417中的无效起始字节-> Python.Runtime.PythonException: UnicodeDecodeError:'utf-8‘编解码器无法解码位置为57405的字节0xa0 : Python.Runtime.PyObject.Invoke上无效的开始字节(Python.Runtime.PyTuple args,Python.Runtime.PyDict kw) 0x00033 in <7ada479175184ff388929ece541bbdb4>:0 at Python.Runtime.PyObject.InvokeMethod (System.String name,Python.Runtime.PyTuple args,Python.Runtime.PyDict kw) 0x00007 in <7ada479175184ff388929ece541bbdb4>:0 at Python.Runtime.PyObject.TryInvokeMember (System.Dynamic.InvokeMemberBinder绑定,System.Object[] args,System.Object&结果) 0x0003e in <7ada479175184ff388929ece541bbdb4>:0 at (包装动态-方法) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,( QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData (QuantConnect.Data.Slice slice) 0x00088 in :0 at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket作业,QuantConnect.Interfaces.IAlgorithm算法,QuantConnect.Lean.Engine.DataFeeds.IDataFeed feed,QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler事务,QuantConnect.Lean.Engine.Results.IResultHandler结果,QuantConnect.Lean.Engine.RealTime.IRealTimeHandler实时,QuantConnect.Lean.Engine.Server.ILeanManager leanManager,QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas,System.Threading.CancellationToken令牌) 0x013e5 in :0 --内部异常堆栈跟踪的结束
发布于 2018-08-02 13:51:38
在用Python或任何语言捕捉异常时,您需要非常清楚要捕获哪些异常,否则程序仍然会崩溃。您正在捕获Exception,但是您的程序正在从UnicodeDecodeError崩溃,因此您应该尝试捕捉该错误并适当地处理它。
试试像这样的except UnicodeDecodeError as e:
https://stackoverflow.com/questions/51655285
复制相似问题