traceback as string

Stephen Ferg steve at ferg.org
Thu Jan 15 12:47:20 EST 2004


I haven't been using this function for very long, but it seems to work. 

You pass it the exception object, and it returns a string.

===================================================

def exception_format(e):
	"""Convert an exception object into a string,
	complete with stack trace info, suitable for display.
	"""
	import traceback
	info = "".join(traceback.format_tb(sys.exc_info()[2]))
	return str(e) + "\n\n" + info

====================================================

Here's an example of how to use it.

==================================================

try:
	main1()
except Exception, e:
	print exception_format(e)

====================================================



More information about the Python-list mailing list