How to catch and print the full exception traceback without halting/exiting the program?

后端 未结 15 2244
天命终不由人
天命终不由人 2020-11-22 13:46

I want to catch and log exceptions without exiting, e.g.,

try:
    do_stuff()
except Exception, err:
    print(Exception, err)
    # I want to print the entir         


        
15条回答
  •  一生所求
    2020-11-22 14:08

    traceback.format_exc() or sys.exc_info() will yield more info if that's what you want.

    import traceback
    import sys
    
    try:
        do_stuff()
    except Exception:
        print(traceback.format_exc())
        # or
        print(sys.exc_info()[2])
    

提交回复
热议问题