Python 重置当前异常链

Python 重置当前异常链,python,python-3.x,Python,Python 3.x,我的应用程序中有以下代码: try: return self.wsgi_app(environ, start_response) except Exception: logger.warning('Failed to render the custom %s page', exc.code, exc_info=True) 当我的MongoDB宕机时,我有: 18:44:55 WARNING Failed to render the custom 500 page Traceba

我的应用程序中有以下代码:

try:
    return self.wsgi_app(environ, start_response)
except Exception:
    logger.warning('Failed to render the custom %s page', exc.code, exc_info=True)
当我的MongoDB宕机时,我有:

18:44:55 WARNING Failed to render the custom 500 page
Traceback (most recent call last):
...
  File ".../venv/lib/python3.5/site-packages/pymongo/topology.py", line 189, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
我不需要关于第一个异常的信息——我以前已经将其记录在我的代码中了。我只希望记录try/except块内发生的异常(显示在“在处理上述异常期间,发生了另一个异常:”之后的异常)

如何在try/except块之前重置当前异常链

我可以试试:

try:
    ...
except Exception as exc:
    exc.__cause__ = None
    logger.exception(...)

但是它会破坏try/except中可能发生的几个异常的有用链。

不确定这是否是适当的修复,但是您是否尝试过使用
except
而不是
Exception
?@JeandersonBarrosC–ndido:这不会有任何区别。您通常不想使用覆盖的
,除了:
。那么您想记录异常的哪一部分?在处理上述内容时,第一部分或下面的第二部分?那么链什么时候有用,什么时候不有用?通过清除
\uuuu cause\uuu
@warvariuc:是的,因此我的问题是什么构成了你不想破坏的东西,只重置链中你不觉得有用的部分。