'Calling print_exc() on a traceback object result in an AttributeError: 'traceback' object has no attribute 'print_exc'
When executing the following code (trying to open an non-existent file on purpose):
import io, sys
try:
with io.open("THIS_FILE_DOES_NOT_EXIST_SO_IT_RAISES_IO_ERROR", 'r', encoding='utf-8') as inputFile:
while line:
print(line)
except IOError as error:
print("I/O error type=\"{}\" value=\"{}\" traceback=\"{}\")".format(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
print("-------------------")
sys.exc_info()[2].print_exc()
the last line of code prints the traceback but at the same time raises an AttributeError: 'traceback' object has no attribute 'print_exc'
. I believe sys.exc_info()[2] is a traceback object as shown in the frist outputted line below. Shouldn't traceback.print_exc() be callable for sys.exc_info()[2]
?
I/O error type="<class 'FileNotFoundError'>" value="[Errno 2] No such file or directory: 'FileDoesNotExist'" traceback="<traceback object at 0x7f90b529a700>")
-------------------
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'FileDoesNotExist'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'traceback' object has no attribute 'print_exc'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|