DEV Community

Tim Kamanin ๐Ÿš€
Tim Kamanin ๐Ÿš€

Posted on โ€ข Originally published at timonweb.com

2

How to print exception traceback in Python

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)
Enter fullscreen mode Exit fullscreen mode

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/

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay