Enable Dark Mode!
By: Ammu Raj

What is Traceback Module in Python?


Traceback is mainly used to print stack traces of a python program. This package provides a standard interface for users to format and extract as they see fit. It replicates the mannerisms of the python interpreter that occurs during the printing of the stack trace. This is very helpful when you have to imitate and print like the stack trace at any given step. Python stack traces only occur when there is an exception. It includes all the information required for tracking and fixing the exception and is hence very helpful for developers.
Functions in the Module
traceback.print_tb(tb, limit = None, file = None):
Limit stack entries from traceback object tb are printed according to the nature of the limit. It will print up to the limit If the nature of the limit is positive. If the limit is negative, it will print the last abs(limit) entries. If the limits nature is None, all entries are printed. If the file is not attached or is NULL, the output goes to sys. stderr instead of an open file or file-like object to which it can receive it.
traceback.print_exception(etype, value, tb, limit = None, file = None, chain = True): Prints exception information and stack trace records from the traceback tb object to a file. If tb is None, print the Traceback (last last call) header. After tracing the stack, it prints the exception type and value.
traceback.print_stack(f=None, limit=None, file=None).
If the limit is positive, print to limit stack trace entries (starting at the recall point). Otherwise, print the last items of abs(limit). If the limit is omitted or none, all items are printed. Here to specify an alternate stack frame, we can use the optional f argument.
traceback.format_exc(limit=None, chain=True)
It's like print_exc(limit) but returns a string instead of printing to a file.
traceback.walk_tb(tb) : Walk the traceback after tb_next, getting the frame and line number for each frame. This helper is used with StackSummary.extract().
traceback.format_exception_only(etype, value) : Formats the traceback exception part. The arguments are the exception type and a value, such as sys.last_type and sys.last_value. Returns a list of strings, each ending on a newline.
traceback.format_exc(limit = None, chain = True): This is like print_exc(limit) except that it returns a string instead of printing to a file.
traceback.extract_stack(f = None, limit = None): It will extract the raw backtrace from the current stack frame. And also, the return value has the same format as for extract_tb().
Example: A simple program that prints an exception stack trace.
# importing traceback module
import traceback
# declaring  an array
A = [a, b, c,d]
    value = A[m]   
    # printing stack trace
# out of try-except
#  statement for the program continues
#runs after the exception is handled
print("Exception occurred")
Traceback (most recent call last):
  File "C:/cybrosys/odoo.py", line 8, in 
    value = A[m]
IndexError: list index out of range
Exception occurred
TracebackException objects
The TracebackException objects are created from the actual exceptions to catch the data, this is a simple way to print.
class traceback.TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False)
Catch the exception for later rendering. limit, lookup_lines, and capture_locals are the same as for the StackSummary class.
For syntax errors - the name of the file where the error occurred.
It is a TracebackException original __context__.
The Value of  __suppress_context__  from the original exception.
It is a TracebackException original __cause__
It is for syntax errors and shows the line number where the error occurred.
It is for syntax errors and shows the text where the error occurred.
The exc_type is class of the original traceback.
format(*, chain=True)
Format the exception.
If the string is not True, __cause__ and __context__ will not be formatted.
The return value is a generator of strings, each of which ends with a new line and some of which contain internal newlines. The print_exception() is a wrapper around this method. This wrapper just prints the lines to the file.
Generators usually output a single string. However, for SyntaxError exceptions, multiple lines are generated with detailed information (on output) about where the syntax error occurred.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message