0

Here is my code, simplified to illustrate the problem:

import sys
def my_excepthook(exc_type, exc_value, exc_traceback):
    print(exc_traceback.format_exception())
sys.excepthook = my_excepthook
x = 5/0

Python hits another exception while handling the ZeroDivisionError, hence the title of this post.

Looking at my debugger, it's certainly a traceback object. It has four attributes:

  • tb_frame: frame
  • tb_lasti: int
  • tb_lineno: int
  • tb_next: traceback

but no methods. Why?

edit:

Reading the traceback module python docs, I had the misconception that I was reading the traceback object python docs.

how to format traceback objects

1 Answer 1

3

I think you are getting a traceback object confused with the traceback package, which contains a format_exception function.

import sys
import traceback
def my_excepthook(exc_type, exc_value, exc_traceback):
    print(traceback.format_exception(exc_type, exc_value, exc_traceback))
sys.excepthook = my_excepthook
x = 5/0
Sign up to request clarification or add additional context in comments.

1 Comment

Or just traceback.format_exception(exc_value), starting from python 3.10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.