0

我最近从 urlib2 切换到 requests,但我不确定如何处理异常。什么是最佳实践?我当前的代码看起来像这样,但没有任何好处:

try:
    response = requests.get(url)
except requests.ConnectionError , e:
    logging.error('ConnectionError = ' + str(e.code))
    return False
except requests.HTTPError , e:
    logging.error('HTTPError = ' + str(e.reason))
    return False
except requests.Timeout, e:
    logging.error('Timeout')
    return False
except requests.TooManyRedirects:
    logging.error('TooManyRedirects')
    return False
except Exception:
    import traceback
    logging.error('generic exception: ' + traceback.format_exc())
    return False
4

1 回答 1

-2

由于评论看起来很糟糕,您是否尝试过:

try:
    # some code
except Exception as e:
    print e
于 2013-06-07T15:27:13.603 回答