尝试/捕获无法与异常名称一起使用

时间:2019-03-13 14:58:17

标签: python try-catch

我无法捕获似乎是ConnectionError的错误。这不起作用:

except ConnectionError:
            return {
                'error_status_code': 500,
                'error_message': "Activity Stream connection failed",
            }

但这确实可行:

except Exception as e:
        if(e.__class__.__name__ == 'ConnectionError'):
            return {
                'error_status_code': 500,
                'error_message': "Activity Stream connection failed",
            }
        else:
            raise e

使用pdb.set_trace()打印e时,我得到:

ConnectionError(MaxRetryError("HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /v1/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10f00e278>: Failed to establish a new connection: [Errno 61] Connection refused',))",),)

建议它是一个ConnectionError。我也尝试捕获MaxRetryError和NewConnectionError无效。

谢谢

0 个答案:

没有答案