簡體   English   中英

try代碼塊中的Python代碼崩潰

[英]Python code crashes within try except block

我正在使用雲平台來運行程序,當它遇到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'編解碼器無法解碼位置57360中的字節0xa0:無效的起始字節

運行時錯誤:UnicodeDecodeError:'utf-8'編解碼器無法解碼位置57405的字節0xa0:main.py:line 458的GetVix在main.py:line 417的OnData處的無效起始字節UnicodeDecodeError:'utf-8'編解碼器無法解碼位置57405中的字節0xa0:無效的起始字節堆棧跟蹤:System.Exception:UnicodeDecodeError:'utf-8'編解碼器無法解碼位置57405中的字節0xa0:main.py:line 417中OnData處的無效起始字節在main.py:第458行的GetVix上---> Python.Runtime.PythonException:UnicodeDecodeError:'utf-8'編解碼器無法解碼位置57405的字節0xa0:Python.Runtime.PyObject.Invoke處的無效起始字節.Runtime.PyTuple args,Python.Runtime.PyDict kw)[0x00033]在Python.Runtime.PyObject.InvokeMethod的<7ada479175184ff388929ece541bbdb4>:0中(System.String名稱,Python.Runtime.PyTuple args,Python.Runtime.PyDict kw)在Python.Runtime.PyObject.TryInvokeMember(System.Dynamic.InvokeMemberBinder活頁夾,System.Object [] args,<7ada479175184ff388929ece541bbdb4>:0中的[0x00007] <7ada479175184ff388929ece541bbdb4>:0中的(System.Object&result)[0x0003e](包裝器動態方法)System.Object:CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,QuantConnect.Data QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData上的.Slice(QuantConnect.Data.Slice slice)[0x00088]在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中-內部異常堆棧跟蹤的結尾-

在Python或與此相關的任何語言中捕獲異常時,您需要非常清楚要捕獲哪些異常,否則程序仍然會崩潰。 您正在捕獲Exception但是您的程序由於UnicodeDecodeError而崩潰,因此您應該嘗試捕獲該錯誤並進行適當處理。

嘗試像except UnicodeDecodeError as e:這樣的東西except UnicodeDecodeError as e:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM