Extract traceback info from an exception object

后端 未结 5 1568
忘掉有多难
忘掉有多难 2020-12-02 10:31

Given an Exception object (of unknown origin) is there way to obtain its traceback? I have code like this:

def stuff():
   try:
       .....
       return us         


        
5条回答
  •  醉酒成梦
    2020-12-02 11:06

    A way to get traceback as a string from an exception object in Python 3:

    import traceback
    
    # `e` is an exception object that you get from somewhere
    traceback_str = ''.join(traceback.format_tb(e.__traceback__))
    

    traceback.format_tb(...) returns a list of strings. ''.join(...) joins them together. For more reference, please visit: https://docs.python.org/3/library/traceback.html#traceback.format_tb

提交回复
热议问题