When a Python segment USES
try... except... After
, I don’t know how to locate the detailed program crush
. These two days, the program needs to use this aspect, so I looked it up.
You need to use the Traceback package
import traceback
try:
#Dividing by zero errors is an example
3/0
except Exception, e:
#This is the output for the error category, which is not really visible if the catch is a generic error
print 'str(Exception):\t', str(Exception) #Output str(Exception): <type 'exceptions.Exception'>
#This is the specific reason for the output error.
print 'str(e):\t\t', str(e) #output str(e): integer division or modulo by zero
print 'repr(e):\t', repr(e) # Output repr(e): ZeroDivisionError('integer division or modulo by zero',)
print 'traceback.print_exc():';
#Both of the following steps output the exact location of the error
traceback.print_exc()
print 'traceback.format_exc():\n%s' % traceback.format_exc()
In addition, Python 2.6 except
after the sentence can be replaced with except Exception as e