简体   繁体   中英

Python code crashes within try except block

I am using a cloud platform to run a program and my code crashes when it runs into an error that is within a try/except block. I do not know if it is due to the platform, but I need a way to avoid the program from crashing.

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 ---

When catching an exception in Python, or any language for that matter, you need to be very clear which exceptions to catch or your program will still crash. You're catching Exception but your program is crashing from a UnicodeDecodeError so you should try catching that error and handling it appropriately.

Try something like this except UnicodeDecodeError as e:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM