【发布时间】:2018-05-09 11:31:59
【问题描述】:
我试图理解为什么堆栈跟踪会被打印事件 虽然异常被捕获。以下是不同方法的示例:
方法 1
import soco
try:
... code that can result with the exception...
except soco.exceptions.SoCoUPnPException as e:
logger.warning("Exception caught. Not expecting trace")
方法 2
from soco.exceptions import SoCoUPnPException
try:
... code that can result with the exception...
except SoCoUPnPException as e:
logger.warning("Exception caught. Not expecting trace")
方法 3
try:
... code that can result with the exception...
except Exception as e:
logger.warning("Exception caught. Not expecting trace")
它们都导致相同的错误:
ERROR [soco.services:410] UPnP Error 701 received: Transition not available from 10.10.10.114
Traceback (most recent call last):
File "/usr/lib/python3.5/site-packages/soco/services.py", line 408, in send_command
self.handle_upnp_error(response.text)
File "/usr/lib/python3.5/site-packages/soco/services.py", line 469, in handle_upnp_error
error_xml=xml_error
soco.exceptions.SoCoUPnPException: UPnP Error 701 received: Transition not available from 10.10.10.114
WARNING [senic_hub.nuimo_app.components.sonos:180] Exception caught. Not expecting trace
我希望只打印 logger.warning("Exception caught. Not expecting trace") 部分而不是整个跟踪。
更新
删除了try/catch 语句,在这种情况下soco.exceptions.SoCoUPnPException 会被提升两次。
异常代码:https://github.com/SoCo/SoCo/blob/release-0.14/soco/exceptions.py#L22
我在这里错过了什么?
【问题讨论】:
-
仅
except:? -
@Mika72 鉴于
Exception caught. Not expecting trace确实被打印出来,似乎在所有三种情况下都成功捕获了异常。也许这是一个库问题,而不是代码中处理异常的方式......
标签: python-3.x exception exception-handling