Tim Kamanin 🚀
Posted on November 20, 2020
Sometimes you're deep down in the code, and you need to debug an error quickly, send a traceback somewhere or log it into a file.
Here's how you can print the traceback of an error in the Python:
import traceback
try:
raise Boom('This is where our code blows')
except Exception:
# here's how you get a traceback output
traceback_output = traceback.format_exc()
# Now you can print it, or send it, or save it in a file
print(traceback_output)
It's a quick and dirty solution, and you probably should be using more sophisticated ways of debugging like error logging, but sometimes you don't have a choice.
Hope it helps someone, let me know by tweeting @timonweb.
Source: https://timonweb.com/python/how-print-traceback-exception-python/
💖 💪 🙅 🚩
Tim Kamanin 🚀
Posted on November 20, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
webdev Devtools Startup Ideas: Building an AI-Powered Debugging Assistant With Code Samples!
November 12, 2024