Pergunta

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
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top