【问题标题】:Catching multiple exceptions - Python捕获多个异常 - Python
【发布时间】:2015-05-19 20:29:14
【问题描述】:

我有一个程序偶尔会抛出一个 badStatusLine 异常,在捕获它之后,我们现在又遇到了另一个错误,我似乎无法捕获它,所以程序不会停止。这是我所拥有的,任何帮助将不胜感激。

错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Users/mattduhon/trading4.py", line 30, in trade
    execution.execute_order(event)
  File "/Users/mattduhon/execution.py", line 33, in execute_order
    params, headers
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1001, in request
    self._send_request(method, url, body, headers)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1029, in _send_request
    self.putrequest(method, url, **skips)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 892, in putrequest
    raise CannotSendRequest()
CannotSendRequest

负责捕获错误的文件:

import httplib
import urllib
from httplib import BadStatusLine
from httplib import CannotSendRequest

class Execution(object):
    def __init__(self, domain, access_token, account_id):
        self.domain = domain
        self.access_token = access_token
        self.account_id = account_id
        self.conn = self.obtain_connection()

    def obtain_connection(self):
        return httplib.HTTPSConnection(self.domain)

    def execute_order(self, event):
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Bearer " + self.access_token}
        params = urllib.urlencode({
            "instrument" : event.instrument,
            "units" : event.units,
            "type" : event.order_type,
            "side" : event.side,
            "stopLoss" : event.stopLoss,
            "takeProfit" : event.takeProfit
        })
        self.conn.request(
            "POST",
            "/v1/accounts/%s/orders" % str(self.account_id),
            params, headers)
        try:
            response = self.conn.getresponse().read()  
        except BadStatusLine as e:
            print(e)
        except CannotSendRequest as a:  ######my attempt at catching the error
            print(a)
        else:          
            print response

【问题讨论】:

    标签: api python-2.7 streaming


    【解决方案1】:

    如果你把最后的else改成:

    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise
    

    如果它真的来自 try-catch 块,你应该得到真正的未捕获错误。但是你确定你没有进入除该块之外的不良状态吗?

    【讨论】:

    • 不,我不确定,我只是想解决抛出的错误。如果程序在块外异常,我该怎么办?感谢您的帮助!
    • 如果将 try 块上移一行以包含请求怎么办?现在还没有发现,但我认为它不是异常安全的
    • 我也会尝试一下。我认为问题出在其他地方,就像你说的那样。就在我收到此错误之前,我正在访问来自 API 的流输出'',就是这样,然后我收到一个错误..
    猜你喜欢
    • 2020-05-10
    • 2013-05-21
    • 1970-01-01
    • 2010-09-13
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多