除块

时间:2018-08-02 13:46:24

标签: python

我正在使用云平台来运行程序,当它遇到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():计划中发生错误   事件QuantConnect.Scheduling.ScheduledEvent。错误是   UnicodeDecodeError:'utf-8'编解码器无法解码位置的字节0xa0   57360:无效的起始字节

     

运行时错误:UnicodeDecodeError:'utf-8'编解码器无法解码字节   位置57405中的0xa0:main.py:line中OnData的无效起始字节   main.py:Line 458上的GetVix处为417 UnicodeDecodeError:'utf-8'编解码器   无法解码位置57405中的字节0xa0:无效的起始字节堆栈   跟踪:System.Exception:UnicodeDecodeError:'utf-8'编解码器无法   解码位置57405中的字节0xa0:InData中的无效起始字节   main.py:第458行的GetVix的main.py:第417行--->   Python.Runtime.PythonException:UnicodeDecodeError:'utf-8'编解码器   无法解码位置57405的字节0xa0:起始字节无效   Python.Runtime.PyObject.Invoke(Python.Runtime.PyTuple args,   Python.Runtime.PyDict kw)[0x00033]在   <7ada479175184ff388929ece541bbdb4>:0在   Python.Runtime.PyObject.InvokeMethod(System.String名称,   Python.Runtime.PyTuple args,Python.Runtime.PyDict kw)[0x00007]在   <7ada479175184ff388929ece541bbdb4>:0在   Python.Runtime.PyObject.TryInvokeMember   (System.Dynamic.InvokeMemberBinder绑定程序,System.Object []参数,   System.Object&result)[0x0003e] in   <7ada479175184ff388929ece541bbdb4>:0 at(包装动态方法)   System.Object:CallSite.Target   (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,对象,QuantConnect.Data.Slice)   在   QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData   (QuantConnect.Data.Slice slice)[0x00088]在:0处   QuantConnect.Lean.Engine.AlgorithmManager.Run   (QuantConnect.Packets.AlgorithmNodePacket作业,   QuantConnect.Interfaces.IAlgorithm算法,   QuantConnect.Lean.Engine.DataFeeds.IDataFeed供稿,   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],位于:0 ---结束   内部异常堆栈跟踪---

1 个答案:

答案 0 :(得分:0)

在Python或与此相关的任何语言中捕获异常时,您需要非常清楚要捕获哪些异常,否则程序仍然会崩溃。您正在捕获Exception,但是程序正在从UnicodeDecodeError中崩溃,因此您应该尝试捕获该错误并进行适当处理。

尝试类似except UnicodeDecodeError as e:

的方法
相关问题