我想达到这样的效果:
def foo():
try:
raise IOError('Stuff ')
except:
raise
def bar(arg1):
try:
foo()
except Exception as e:
e.message = e.message + 'happens at %s' % arg1
raise
bar('arg1')
Traceback...
IOError('Stuff Happens at arg1')
但我得到的是:
Traceback..
IOError('Stuff')
关于如何实现这一点,有什么线索吗?如何在Python 2和3中都做到这一点?
我使用的一个方便的方法是使用类属性作为详细信息的存储,因为类属性可以从类对象和类实例中访问:
class CustomError(Exception):
def __init__(self, details: Dict):
self.details = details
然后在代码中:
raise CustomError({'data': 5})
当捕获错误时:
except CustomError as e:
# Do whatever you want with the exception instance
print(e.details)
与之前的答案不同,这适用于非常糟糕的__str__异常。
但是,它确实修改了类型,以便剔除无用的__str__实现。
我仍然希望找到一个不修改类型的额外改进。
from contextlib import contextmanager
@contextmanager
def helpful_info():
try:
yield
except Exception as e:
class CloneException(Exception): pass
CloneException.__name__ = type(e).__name__
CloneException.__module___ = type(e).__module__
helpful_message = '%s\n\nhelpful info!' % e
import sys
raise CloneException, helpful_message, sys.exc_traceback
class BadException(Exception):
def __str__(self):
return 'wat.'
with helpful_info():
raise BadException('fooooo')
原始的回溯和类型(名称)被保留。
Traceback (most recent call last):
File "re_raise.py", line 20, in <module>
raise BadException('fooooo')
File "/usr/lib64/python2.6/contextlib.py", line 34, in __exit__
self.gen.throw(type, value, traceback)
File "re_raise.py", line 5, in helpful_info
yield
File "re_raise.py", line 20, in <module>
raise BadException('fooooo')
__main__.BadException: wat.
helpful info!
到目前为止,我对所有给出的答案都不满意。他们还是太啰嗦了。在代码和消息输出中。
所有我想要的是stacktrace指向源异常,没有异常之间的东西,所以没有创建新的异常,只是重新提升原始与所有相关的堆栈帧状态在它,导致那里。
史蒂夫·霍华德给出了一个很好的答案,我想扩展一下,不,是简化一下……仅适用于python 3。
except Exception as e:
e.args = ("Some failure state", *e.args)
raise
唯一的新东西是参数展开/解包,这使得它对我来说足够小和容易使用。
试一试:
foo = None
try:
try:
state = "bar"
foo.append(state)
except Exception as e:
e.args = ("Appending '"+state+"' failed", *e.args)
raise
print(foo[0]) # would raise too
except Exception as e:
e.args = ("print(foo) failed: " + str(foo), *e.args)
raise
这将给你:
Traceback (most recent call last):
File "test.py", line 6, in <module>
foo.append(state)
AttributeError: ('print(foo) failed: None', "Appending 'bar' failed", "'NoneType' object has no attribute 'append'")
简单的漂亮图案可以是这样的
print("\n".join( "-"*i+" "+j for i,j in enumerate(e.args)))
我将提供一段我经常使用的代码片段,每当我想向异常添加额外信息时。我在Python 2.7和3.6中都可以工作。
import sys
import traceback
try:
a = 1
b = 1j
# The line below raises an exception because
# we cannot compare int to complex.
m = max(a, b)
except Exception as ex:
# I create my informational message for debugging:
msg = "a=%r, b=%r" % (a, b)
# Gather the information from the original exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
# Format the original exception for a nice printout:
traceback_string = ''.join(traceback.format_exception(
exc_type, exc_value, exc_traceback))
# Re-raise a new exception of the same class as the original one,
# using my custom message and the original traceback:
raise type(ex)("%s\n\nORIGINAL TRACEBACK:\n\n%s\n" % (msg, traceback_string))
上面的代码产生如下输出:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-09b74752c60d> in <module>()
14 raise type(ex)(
15 "%s\n\nORIGINAL TRACEBACK:\n\n%s\n" %
---> 16 (msg, traceback_string))
TypeError: a=1, b=1j
ORIGINAL TRACEBACK:
Traceback (most recent call last):
File "<ipython-input-6-09b74752c60d>", line 7, in <module>
m = max(a, b) # Cannot compare int to complex
TypeError: no ordering relation is defined for complex numbers
我知道这与问题中提供的例子有一点偏差,但我希望有人觉得它有用。