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.
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
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:
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().