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
There are times when an explicit ... a common handler function, but avoid passing the exception object to it explicitly. You can get the current exception for a thread by calling exc_info()....
Andrea Minini
andreaminini.net › computer-science › python › sysexc_info-function-in-python
Sys.exc_info() Function in Python - Andrea Minini
The exc_info() function returns information about the most recent exception that was raised. It is particularly useful for handling unexpected errors within a try except block. Note: Since this is a function from an external library (sys), to use it in a Python script, you need to import the ...
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
log_filename = "exception_information_files/excep_in_excep.log" logging.basicConfig( level=logging.INFO, filename=log_filename, filemode="w", format="%(asctime)s %(levelname)s %(message)s" ) def inside_exception(): try: 7/0 except: pass def outside_exception(): try: inside_exception() "hello" + 7 except: logging.error("Any error", exc_info=True) outside_exception() with open(log_filename) as f: print(f.read())
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › sys.exc_info.html
sys.exc_info() — Python Standard Library
sys.exc_info() -> (type, value, traceback)¶ · Return information about the most recent exception caught by an except clause in the current stack frame or in an older stack frame.
Author: Alex Martelli
Published: 2003
Pages: 656
UTK
web.eecs.utk.edu › ~bvanderz › cs365 › notes › Python › PythonExceptionHandling.html
exception handling
This code is non-protected code (i.e., code we do not expect to cause an exception) finally: code executed regardless of whether an exception occurs, and regardless of whether an exception is handled if one occurs Here is an example from the Python tutorial: import sys try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) except IOError as e: print ("I/O error({0}): {1}".format(e.errno, e.strerror)) except ValueError: print ("Could not convert data to an integer.") except: print ("Unexpected error:", sys.exc_info()[0]) raise The try mechanism operates as follows: