【发布时间】:2011-10-23 14:24:41
【问题描述】:
我目前正在编写一个包装类。我希望能够正确记录异常,但允许调用方法知道发生的异常。我的班级是这样的:
import logging
log = logging.getLogger('module')
class MyAPIWrapper(library.APIClass):
def __init__(self):
self.log = logging.getLogger('module.myapiwrapper')
def my_wrapper_method(self):
try:
response = self.call_api_method()
return response.someData
except APIException, e:
self.log.exception('Oh noes!')
raise e #Throw exception again so calling code knows it happened
我有点怀疑捕获和异常只是为了记录它然后重新引发它,以便调用代码可以做一些事情。这里的正确模式是什么?
【问题讨论】:
-
python exception logging 的可能重复项
-
这正是我正在做的。感谢您发布这个问题。
标签: python exception logging design-patterns