我想达到这样的效果:

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中都做到这一点?


当前回答

到目前为止,我对所有给出的答案都不满意。他们还是太啰嗦了。在代码和消息输出中。

所有我想要的是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)))

其他回答

假设你不想或不能修改foo(),你可以这样做:

try:
    raise IOError('stuff')
except Exception as e:
    if len(e.args) >= 1:
        e.args = (e.args[0] + ' happens',) + e.args[1:]
    raise

这确实是在Python 3中解决问题的唯一解决方案,而不会出现丑陋而令人困惑的“在处理上述异常期间,发生了另一个异常”消息。

如果要将重新提升的行添加到堆栈跟踪中,则应该写入raise e而不是raise。

到目前为止,我对所有给出的答案都不满意。他们还是太啰嗦了。在代码和消息输出中。

所有我想要的是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)))

以下是我在个人项目中使用的方法(我相信有足够的理由不在产品代码中这样做):

try:
    #something hazardous
except Exception as e:
    insightful_message = "shouldn't have done that"
    amended_args = tuple([f'{e.args[0]}\n{insightful_message}', *e.args[1:]])
    e.args = amended_args
    raise

代码(1)拦截错误;(2)创建错误的.args属性的副本,这是一个元组,假定在索引0处包含一个错误消息,使用列表理解实现;(3)在错误消息后追加换行符和自定义消息;(4)使用。args附加到副本的任何附加项 拆包;(5)将副本转换为元组;最后(6)用修改后的副本替换。args。

这些操作大多是为了避免.args元组的不可变性。

我在代码中使用:

try:
    a=1
    b=0
    c=a/b

except:
    raise Exception(f"can't divide {a} with {b}")

输出:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11708/1469673756.py in <module>
      3     b=0
----> 4     c=a/b
      5 

ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11708/1469673756.py in <module>
      5 
      6 except Exception:
----> 7     raise Exception(f"can't divide {a} with {b}")

Exception: can't divide 1 with 0

这是我的实现,使用它作为上下文管理器,并可选地添加额外的消息异常:

from typing import Optional, Type
from types import TracebackType

class _addInfoOnException():
    def __init__(self, info: str = ""):
        self.info = info

    def __enter__(self):
        return

    def __exit__(self,
                 exc_type: Optional[Type[BaseException]],
                 exc_val: BaseException,  # Optional, but not None if exc_type is not None
                 exc_tb: TracebackType):  # Optional, but not None if exc_type is not None
        if exc_type:
            if self.info:
                newMsg = f"{self.info}\n\tLow level error: "
                if len(exc_val.args) == 0:
                    exc_val.args = (self.info, )
                elif len(exc_val.args) == 1:
                    exc_val.args = (f"{newMsg}{exc_val.args[0]}", )
                elif len(exc_val.args) > 0:
                    exc_val.args = (f"{newMsg}{exc_val.args[0]}", exc_val.args[1:])
            raise

用法:

def main():
    try:
        raise Exception("Example exception msg")
    except Exception:
        traceback.print_exc()
        print("\n\n")

    try:
        with _addInfoOnException():
            raise Exception("Example exception msg, no extra info")
    except Exception:
        traceback.print_exc()
        print("\n\n")

    try:
        with _addInfoOnException("Some extra info!"):
            raise Exception("Example exception msg")
    except Exception:
        traceback.print_exc()
        print("\n\n")


if __name__ == "__main__":
    main()

这将在这样的跟踪中解决:

Traceback (most recent call last):
  File "<...>\VSCodeDevWorkspace\testis.py", line 40, in main
    raise Exception("Example exception msg")
Exception: Example exception msg



Traceback (most recent call last):
  File "<...>\VSCodeDevWorkspace\testis.py", line 47, in main
    raise Exception("Example exception msg, no extra info")
  File "<...>\VSCodeDevWorkspace\testis.py", line 47, in main
    raise Exception("Example exception msg, no extra info")
Exception: Example exception msg, no extra info



Traceback (most recent call last):
  File "<...>\VSCodeDevWorkspace\testis.py", line 54, in main
    raise Exception("Example exception msg")
  File "<...>\VSCodeDevWorkspace\testis.py", line 54, in main
    raise Exception("Example exception msg")
Exception: Some extra info!
        Low level error: Example exception msg