When I handle an exception in python
JavaScript
x
6
1
try:
2
a = dict()
3
a[1]
4
except Exception as e:
5
print str(e)
6
It prints
JavaScript
1
2
1
1
2
I expect it to print
JavaScript
1
2
1
KeyError: 1
2
Is there a way to retrieve the default error message ?
Advertisement
Answer
Instead of this:
JavaScript
1
2
1
print str(e)
2
do this:
JavaScript
1
2
1
print(type(e).__name__ + ": " + str(e))
2
or just this:
JavaScript
1
2
1
print(type(e).__name__, e)
2