Voglio salvare il nome dell'errore e i dettagli del traceback in una variabile. Ecco il mio tentativo.
import sys
try:
try:
print x
except Exception, ex:
raise NameError
except Exception, er:
print "0", sys.exc_info()[0]
print "1", sys.exc_info()[1]
print "2", sys.exc_info()[2]
Produzione:
0 <type 'exceptions.NameError'>
1
2 <traceback object at 0xbd5fc8>
Uscita desiderata:
0 NameError
1
2 Traceback (most recent call last):
File "exception.py", line 6, in <module>
raise NameError
PS So che questo può essere fatto facilmente usando il modulo traceback, ma voglio sapere qui come usare l'oggetto sys.exc_info () [2].
<python install path>/Lib/traceback.py) per maggiori informazioni.