If you want to print an exception stack trace in Python like the way it's done in other programming languages such as Java, you can make use of the traceback module.
Example:
import traceback
try:
sum = 1/0
except Exception as e:
traceback.print_exc()
Output:
Traceback (most recent call last):
File "<ipython-input-7-33e8636468b8>", line 4, in <cell line: 3>
sum = 1/0
ZeroDivisionError: division by zero
You get details like the line number, the exact line, the exception type, and the exception description, printed on the console.
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!