[Python] Python code crashes within try except block

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

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

    Continue reading...

Compartilhe esta Página