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

后端 未结 15 2355
天命终不由人
天命终不由人 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:04

    In addition to @Aaron Hall's answer, if you are logging, but don't want to use logging.exception() (since it logs at the ERROR level), you can use a lower level and pass exc_info=True. e.g.

    try:
        do_something_that_might_error()
    except Exception:
        logger.info('General exception noted.', exc_info=True)
    

提交回复
热议问题