Get exception description and stack trace which caused an exception, all as a string

前端 未结 11 1932
旧巷少年郎
旧巷少年郎 2020-11-30 16:22

I\'ve seen a lot of posts about stack trace and exceptions in Python. But haven\'t found what I need.

I have a chunk of Python 2.7 code that may raise an exception.

11条回答
  •  自闭症患者
    2020-11-30 16:38

    my 2-cents:

    import sys, traceback
    try: 
      ...
    except Exception, e:
      T, V, TB = sys.exc_info()
      print ''.join(traceback.format_exception(T,V,TB))
    

提交回复
热议问题