3

I am trying to log the error that caused my process to shut down, but traceback.print_tb and traceback.print_exc don't seem to realize what the raised error is.

In other words, why does the following print 'None\n' instead of 'DivisionByZero .. etc ..'? (Edit: ..and how do I get access to the raised/handled error from within exit_fn?)

import traceback, atexit

def exit_fn():
    print 'exiting'
    if traceback.format_exc().startswith('None'):
        print 'why is this None?'

atexit.register(exit_fn)

x = 1/0
Paul
  • 2,973
  • 6
  • 31
  • 40
  • This may help: http://stackoverflow.com/questions/9741351/how-to-find-exit-code-or-reason-when-atexit-callback-is-called-in-python – e9t Jun 29 '15 at 09:16

1 Answers1

1

I believe that, by the time your routine has been called, the exception has already been 'handled' (at least when I run your code, I see a traceback even if I remove your call to print one), so that there is none to be formatted at that point.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101