我正在使用云平台运行一个程序,当它遇到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 = 0
LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event QuantConnect.Scheduling.ScheduledEvent. The error was UnicodeDecodeError : 'utf-8' codec can't decode byte 0xa0 in position 57360: invalid start byte
Runtime Error: UnicodeDecodeError : 'utf-8' codec can't decode byte 0xa0 in position 57405: invalid start byte at OnData in main.py:line 417 at GetVix in main.py:line 458 UnicodeDecodeError : 'utf-8' codec can't decode byte 0xa0 in position 57405: invalid start byte Stack Trace: System.Exception: UnicodeDecodeError : 'utf-8' codec can't decode byte 0xa0 in position 57405: invalid start byte at OnData in main.py:line 417 at GetVix in main.py:line 458 ---> Python.Runtime.PythonException: UnicodeDecodeError : 'utf-8' codec can't decode byte 0xa0 in position 57405: invalid start byte at 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 binder, System.Object[] args, System.Object& result) [0x0003e] in <7ada479175184ff388929ece541bbdb4>:0 at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,QuantConnect.Data.Slice) at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData (QuantConnect.Data.Slice slice) [0x00088] in :0 at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.IDataFeed feed, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x013e5] in :0 --- End of inner exception stack trace ---
在Python或其他语言中捕捉异常时,需要非常清楚地知道要捕捉哪些异常,否则程序仍将崩溃。您正在捕获
Exception
,但是您的程序正在从UnicodeDecodeError
崩溃,因此您应该尝试捕获该错误并进行适当的处理。在试试这样的方法
except UnicodeDecodeError as e:
相关问题 更多 >
编程相关推荐