How to print exception stack trace in Python?
Last Updated :
10 Jul, 2020
Prerequisite: Python Traceback
To print stack trace for an exception the suspicious code will be kept in the try block and except block will be employed to handle the exception generated. Here we will be printing the stack trace to handle the exception generated. The printing stack trace for an exception helps in understanding the error and what went wrong with the code. Not just this, the stack trace also shows where the error occurred.
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
Method 1: By using print_exc() method.
This method prints exception information and stack trace entries from traceback object tb to file.
Syntax: traceback.print_exc(limit=None, file=None, chain=True)
Parameters: This method accepts the following parameters:
- if a limit argument is positive, Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame). Otherwise, print the last abs(limit) entries. If the limit argument is None, all entries are printed.
- If the file argument is None, the output goes to sys.stderr; otherwise, it should be an open file or file-like object to receive the output.
- If chain argument is true (the default), then chained exceptions will be printed as well, like the interpreter itself does when printing an unhandled exception.
Return: None.
Code:
Python3
import traceback
A = [ 1 , 2 , 3 , 4 ]
try :
value = A[ 5 ]
except :
traceback.print_exc()
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
Method 2: By using print_exception() method.
This method prints exception information and stack trace entries from traceback object tb to file.
Syntax : traceback.print_exception(etype, value, tb, limit=None, file=None, chain=True)
Parameters: This method accepts the following parameters:
- if tb argument is not None, it prints a header Traceback (most recent call last):
- it prints the exception etype and value after the stack trace
- if type(value) argument is SyntaxError and value has the appropriate format, it prints the line where the syntax error occurred with a caret indicating the approximate position of the error.
- if a limit argument is positive, Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame). Otherwise, print the last abs(limit) entries. If the limit argument is None, all entries are printed.
- If the file argument is None, the output goes to sys.stderr; otherwise, it should be an open file or file-like object to receive the output.
- If chain argument is true (the default), then chained exceptions will be printed as well, like the interpreter itself does when printing an unhandled exception.
Return: None.
Code:
Python3
import traceback
import sys
a = 4
b = 0
try :
value = a / b
except :
traceback.print_exception( * sys.exc_info())
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
Please Login to comment...