This is how I do it:
>>> import traceback
>>> try:
... int('k')
... except:
... var = traceback.format_exc()
...
>>> print(var)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: invalid literal for int() with base 10: 'k'
You should however take a look at the traceback documentation, as you might find there more suitable methods, depending to how you want to process your variable afterwards...
Answer from mac on Stack OverflowTutorialspoint
tutorialspoint.com › python › python_sys_exc_info_method.htm
Python sys.exc_info() method
The Python sys.exc_info() method that returns a tuple containing information about the most recent exception caught by an except clause. The tuple consists of three elements, they are the exception type, the exception value and a traceback object.
Top answer 1 of 5
266
This is how I do it:
>>> import traceback
>>> try:
... int('k')
... except:
... var = traceback.format_exc()
...
>>> print(var)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: invalid literal for int() with base 10: 'k'
You should however take a look at the traceback documentation, as you might find there more suitable methods, depending to how you want to process your variable afterwards...
2 of 5
40
sys.exc_info() returns a tuple with three values (type, value, traceback).
- Here type gets the exception type of the Exception being handled
- value is the arguments that are being passed to constructor of exception class
- traceback contains the stack information like where the exception occurred etc.
For Example, In the following program
try:
a = 1/0
except Exception,e:
exc_tuple = sys.exc_info()
Now If we print the tuple the values will be this.
- exc_tuple[0] value will be "ZeroDivisionError"
- exc_tuple[1] value will be "integer division or modulo by zero" (String passed as parameter to the exception class)
- exc_tuple[2] value will be "trackback object at (some memory address)"
The above details can also be fetched by simply printing the exception in string format.
print str(e)
Python Module of the Week
pymotw.com › 2 › sys › exceptions.html
Exception Handling - Python Module of the Week
The return value of exc_info() is a three member tuple containing the exception class, an exception instance, and a traceback.
Andrea Minini
andreaminini.net › computer-science › python › sysexc_info-function-in-python
Sys.exc_info() Function in Python - Andrea Minini
In Python, the exc_info() function allows you to obtain information about errors and exceptions that occur during the execution of a script.
Python
docs.python.org › 3 › library › traceback.html
traceback — Print or retrieve a stack traceback
Format the exception part of a traceback using an exception value such as given by sys.last_exc. The return value is a list of strings, each ending in a newline. The list contains the exception’s message, which is normally a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred.
Linuxtopia
linuxtopia.org › online_books › programming_books › python_programming › python_ch17s06.html
Python - Exception Functions
The sys module provides one function that provides the details of the exception that was raised. Programs with exception handling will occasionally use this function · The sys.exc_info function returns a 3-tuple with the exception, the exception's parameter, and a traceback object that pinpoints ...
Fedorkobak
fedorkobak.github.io › python › standard_library › logging › exception_information.html
Exceptions information — Python
By adding the exc_info=True argument to the logging method, you can include information about the exception in the log.
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › sys.exc_info.html
sys.exc_info() — Python Standard Library
Return information about the most recent exception caught by an except clause in the current stack frame or in an older stack frame.
Python Module of the Week
pymotw.com › 2 › traceback
traceback – Extract, format, and print exceptions and stack traces. - Python Module of the Week
import traceback import sys from traceback_example import produce_exception try: produce_exception() except Exception, err: print 'print_exception():' exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_tb) $ python traceback_print_exception.py Traceback (most recent call last): File "traceback_print_exception.py", line 16, in <module> produce_exception() File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exception produce_exception(recursion_level-1) File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 16, in produce_exception produce_exception(recursion_level-1) File "/Users/dhellmann/Documents/PyMOTW/src/PyMOTW/traceback/traceback_example.py", line 18, in produce_exception raise RuntimeError() RuntimeError print_exception():
Author: Alex Martelli
Published: 2003
Pages: 656