In the python and/or ipython interactive interpreter, how can I get name bound on the last unhandled exception? I.e. the equivalent of
>>> try:
... 1/0
... except Exception as potato:
... pass
...
>>> format(potato)
'integer division or modulo by zero'
Must be something like...
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> import sys
>>> potato = ???
You can use sys.last_value
for this:
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> sys.last_value
ZeroDivisionError('integer division or modulo by zero',)
>>> type(sys.last_value)
<type 'exceptions.ZeroDivisionError'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With