Printing Tracebacks


Sometimes you’re in a situation where you need to debug something that is inside a piece of code like this:

try:
	[...]
except Exception as e:
	[...]

An option would be printing e but it will show only the exception message, instead of the traceback. You can solve this by adding:

import traceback


try:
	[...]
except Exception as e:
    [...]
	print(''.join(traceback.format_exception(None, e, e.__traceback__)))

It will show the exception and the traceback.

Please avoid catching generic exceptions. It can disguise many important errors and bring a lot of headache for you in the future.


comments powered by Disqus