How can I print a Python stack trace when an exception is caught, but without re-raising the exception?

Question

Grade: Education Subject: Support
How can I print a Python stack trace when an exception is caught, but without re-raising the exception?
Asked by:
103 Viewed 103 Answers

Answer (103)

Best Answer
(348)
You can use the `traceback` module. Specifically, `traceback.print_stack()` will print the current stack trace to `sys.stderr`. You'll need to catch the exception, print the stack trace, and then continue execution without re-raising it using `pass` or a similar statement. Example: `try: ... except Exception as e: traceback.print_stack(); pass`