Domanda

Sto cercando di registrare l'errore che ha causato lo spegnimento del mio processo, ma traceback.print_tb e traceback.print_exc non sembrano realizzare ciò che l'errore rialzato è.

In altre parole, perché la seguente stampa "nessuno \ n" invece di 'divisionbyzero .. ecc. "?(Modifica: ..e come posso ottenere l'accesso all'errore sollevato / gestito da 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
.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top