Python Program to Print Stack Trace

Overview
A stack trace, also known as stack traceback, traceback, or backtrace, prints all the function calls that took place before an error occurred. Python Print Stack Trace assists users in figuring out the cause of an error and solving it.
What is Stack Trace in Python?
A stack trace is used to display all the function calls before an error occurs. The error is reported on the final line of the stack trace. The stack trace also shows information and function calls related to the thrown error to help users locate and solve it.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
How to Print Stack Trace in Python?
Method 1: Using print_exc()
In the following code snippet, the value of the denominator is set to 0 to view how Python print stack trace works. The traceback.print_exc() method in the except block prints the program's location, the line where the error was encountered, and the name and relevant information about the error. The last line confirms that the entire code executes without hindrance.
Syntax:
Code:
Output:
Method 2: Using print_exception()
In the following example, since the list arr has indices from 0-4 trying to address index 5 throws an error. The traceback.print_exception(*sys.exc_info()) method in the except block prints the details and cause of the error. The last line printed confirms that the entire code executes successfully.
Syntax:
Code:
Output:
More Examples for Understanding
Turn Learning into Career Growth
Example 1
In the following erroneous code snippet, blood_group is written as bloodgroup to see how Python print stack trace works. The displayed stack trace contains information about the type of error - NameError, that occurred because the referenced variable bloodgroup is not defined. The exception tells us that the variable, function, or class does not exist due to incorrect reference.
Code:
Output:
Example 2
In the following code snippet, the value for children is set to 0 to view how Python print stack trace works. The displayed stack trace contains information about the type of error - ZeroDivisionError, that occurred because the denominator is 0.
Code:
Output:
Example 3
In the following example, since the list arr has indices from 0-4 trying to address index 5 throws an error. The traceback.print_exception(*sys.exc_info()) method in the except block prints the details and cause of the error. This example is used to add more depth to the stack for easy visualization. Initially the function h() is called followed by g() and finally f() where trying to address index 5 throws an error.
Code:
Output:
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
FAQs
Q. Which module is required for Python print stack trace?
A. Importing the traceback module helps to extract, format and print stack traces.
Q. What does a stack trace show?
A. A stack trace displays the call stack (set of active method calls) and provides information about the methods called before an error occurs. It helps developers to figure out what went wrong in the code.
Q. What information does the stack trace contain? The stack trace contains :
A.
- Traceback for the most recent call.
- Location of the program.
- Erroneous Line.
- Type of error and relevant information.
Q. What is the difference between print_exc and print_exception?
A.
| print_exc | print_exception |
|---|---|
| traceback.print_exc(limit=None, file=None, chain=True) | traceback.print_exception(etype, value, tb, limit=None, file=None, chain=True) |
| It accepts 3 parameters limit ,file, and chain. | It accepts 6 parameters etype, value, tb, limit, file, chain |
Conclusion
- In Python print stack trace to see the function calls that took place before an error occurred.
- The stack trace information helps to locate and fix the error easily.
- The traceback module in Python provides functionalities to deal with the stack trace.
- The methods used to print stack trace are print_exception() and print_exc().