How to print exception stack trace in Python?

To print stack follow for a special case the dubious code will be kept in the attempt block and with the exception of square will be utilized to deal with the exemption produced. Here we will print the stack follow to deal with the special case produced. The printing stack follow for a special case helps in understanding the blunder and what turned out badly with the code. In addition to this, the stack follow likewise shows where the blunder happened.

The general structure of a stack trace for an exception:

  • Traceback for the most recent call.
  • Location of the program.
  • Line in the program where the error was encountered.
  • Name of the error: relevant information about the exception.

Example: 

Traceback (most recent call last):
  File "C:/Python27/hdg.py", line 5, in 
    value=A[5]
IndexError: list index out of range

Technique 1: By utilizing print_exc() strategy.

This technique prints exemption data and stack follow passages from traceback object tb to document.

Sentence structure: traceback.print_exc(limit=None, file=None, chain=True)

Boundaries: This strategy acknowledges the accompanying boundaries:

assuming that a breaking point contention is positive, Print up to restrict stack follow passages from traceback object tb (beginning from the guest’s casing). In any case, print the last abs(limit) sections. On the off chance that the breaking point contention is None, all sections are printed.
On the off chance that the document contention is None, the result goes to sys.stderr; any other way, it ought to be an open record or record like item to get the result.
In the event that chain contention is valid (the default), then, at that point, binded special cases will be printed also, similar to the actual mediator does when printing an unhandled exemption.
Return: None.

Code:

# import module
import traceback
 
# declaring and assigning array
A = [1, 2, 3, 4]
 
# exception handling
try:
    value = A[5]
     
except:
    # printing stack trace
    traceback.print_exc()
 
# out of try-except
# this statement is to show
# that program continues normally
# after an exception is handled
print("end of program")

Output:

Traceback (most recent call last):
  File "C:/Python27/hdg.py", line 8, in 
    value=A[5]
IndexError: list index out of range
end of program

Strategy 2: By utilizing print_exception() technique.

This strategy prints special case data and stack follow passages from traceback object tb to document.

Sentence structure : traceback.print_exception(etype, esteem, tb, limit=None, file=None, chain=True)

Boundaries: This strategy acknowledges the accompanying boundaries:

assuming that tb contention isn’t None, it prints a header Traceback (latest call last):
it prints the exemption etype and esteem later the stack follow
on the off chance that type(value) contention is SyntaxError and worth has the proper configuration, it prints the line where the grammar blunder happened with a caret demonstrating the estimated position of the mistake.
on the off chance that a cutoff contention is positive, Print up to restrict stack follow sections from traceback object tb (beginning from the guest’s casing). In any case, print the last abs(limit) passages. On the off chance that the cutoff contention is None, all sections are printed.
Assuming the record contention is None, the result goes to sys.stderr; any other way, it ought to be an open document or record like item to get the result.
Assuming that chain contention is valid (the default), then, at that point, binded exemptions will be printed too, similar to the actual translator does when printing an unhandled special case.
Return: None.

Code:

# import required libraries
import traceback
import sys
 
# initialising variables
a = 4
b = 0
 
# exception handling
try:
    value = a / b
 
except:
    # printing stack trace
    traceback.print_exception(*sys.exc_info())
 
# out of try-except
# this statement is to show 
# that program continues
# normally after an exception is handled
print("end of program")

Output:

Traceback (most recent call last):
  File "C:/Python27/hdg.py", line 10, in 
    value=a/b
ZeroDivisionError: integer division or modulo by zero
end of program

Also Read: How to create admin login page using PHP?

Leave a Reply

Your email address will not be published. Required fields are marked *