简体   繁体   English

try代码块中的Python代码崩溃

[英]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. 我正在使用云平台来运行程序,当它遇到try / except块内的错误时,代码崩溃。 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. LiveTradingRealTimeHandler.Run():计划的事件QuantConnect.Scheduling.ScheduledEvent中发生错误。 The error was UnicodeDecodeError : 'utf-8' codec can't decode byte 0xa0 in position 57360: invalid start byte 错误是UnicodeDecodeError:'utf-8'编解码器无法解码位置57360中的字节0xa0:无效的起始字节

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, 运行时错误: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] 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 --- <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中-内部异常堆栈跟踪的结尾-

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. 在Python或与此相关的任何语言中捕获异常时,您需要非常清楚要捕获哪些异常,否则程序仍然会崩溃。 You're catching Exception but your program is crashing from a UnicodeDecodeError so you should try catching that error and handling it appropriately. 您正在捕获Exception但是您的程序由于UnicodeDecodeError而崩溃,因此您应该尝试捕获该错误并进行适当处理。

Try something like this except UnicodeDecodeError as e: 尝试像except UnicodeDecodeError as e:这样的东西except UnicodeDecodeError as e:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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