21
traceback module in Python
So, i thought why not write an article on how to get the Python interpreter like error messages.
I read about it firstly on StackOverflow and from the Python documentations.
So, here we go on a basic tour of exception handling with traceback.
We need to import the python sys
module and traceback
module.
import traceback
import sys
Then we will use the sys
modules sys.exc_info()
method to extract the exception_type
,exception
and traceback
.
And after gettting all these values we will print these using the traceback
modules traceback.print_tb()
method:
import traceback
import sys
try:
a = 8/0
except:
ex_type, ex, tb = system.exc_info()
print(traceback.print_tb(tb))
Outputs:
File "<stdin>", line 2, in <module>
That is a basic use case of traceback
and if you wanna learn further about it you might want to read some stackoverflow answerws and the official python documentation:
21