By calling sys.exc_info() when an exception is handled a 3-tuple is returned containing the exception class, the exception object and the traceback.
This is also evident by the documentation of sys.exc_info:
This function returns a tuple of three values that give information about the exception that is currently being handled. ... the values returned are
(type, value, traceback). ... traceback gets a traceback object (see the Reference Manual) which encapsulates the call stack at the point where the exception originally occurred.
I want to use the type traceback which is used to create the third variable in the aforementioned exc_info return value but can't find where it's defined.
My question is, therefore, where is the traceback type available for python scripts?
EDIT:
I would like to use the traceback type to define a PyQt signal. PyQt Signals are defined by specifying the signal name together with the types of parameters passed. I do not need to create an object of that type, only use it in a manner similar to an isinstance call.
bad news, even if you can get the class of the traceback object like this:
import sys
try:
raise Exception
except:
tb = sys.exc_info()[2]
print(tb.__class__)
result:
<class 'traceback'>
when you try:
tb.__class__()
you get:
TypeError: cannot create 'traceback' instances
so the traceback type cannot be instanciated externally, probably because you'd need to access python internals to do so (and attributes are read-only even tb_lineno so not possible to "reuse" an instance either)
You can use traceback.format_exc() or sys.exc_info() like :
try:
raise TypeError("Error !?!")
except Exception:
print(traceback.format_exc())
# or
print(sys.exc_info()[2])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With