Coder Perfect

Getting the value of an exception in Python

Problem

If I have that code:

try:
    some_method()
except Exception, e:

What is the best way to acquire this Exception value (string representation)?

Asked by Frias

Solution #1

use str

try:
    some_method()
except Exception as e:
    s = str(e)

The args attribute will also be present in most exception classes. args[0] is frequently an error message.

It’s worth noting that if there’s no error message, str will return an empty string, whereas repr, as recommended by pyfunc, will at least display the exception’s class. If you’re printing it out, I assume it’s for a user who doesn’t care about the class and just wants an error message.

It all depends on the type of exception you’re dealing with and how it was created. Did you have anything specific in mind?

Answered by aaronasterling

Solution #2

Using repr:

>>> try:
...     print(x)
... except Exception as e:
...     print(repr(e))
... 
NameError("name 'x' is not defined")

Using str:

>>> try:
...     print(x)
... except Exception as e:
...     print(str(e))
... 
name 'x' is not defined

Answered by pyfunc

Solution #3

Despite the fact that I realize this is an old question, I would recommend utilizing the traceback module to handle error output.

Use traceback.print exc() to print the current exception to standard error, just as if it were still uncaught, or traceback.format exc() to obtain the same output as a string. If you want to limit the output or redirect the printing to a file-like object, you can supply additional arguments to either of those functions.

Answered by Blckknght

Solution #4

Another option hasn’t been mentioned yet:

try:
    1/0
except Exception, e:
    print e.message

Output:

integer division or modulo by zero

It’s possible that args[0] isn’t a message at all.

If unicode is used, str(e) may return the string with surrounding quotes and perhaps the leading u:

'integer division or modulo by zero'

The whole exception representation is returned by repr(e), which is usually not what you want:

"ZeroDivisionError('integer division or modulo by zero',)"

edit

It’s my fault!!! Finally, it appears that BaseException.message has been deprecated since 2.6, and there is still no standardized mechanism to show exception messages. So, depending on your needs, I suppose the best option is to deal with e.args and str(e) (and possibly e.message if the lib you are using is relying on that mechanism).

In pygraphviz, for example, e.message is the only option to display the exception correctly; str(e) will surround the message with u”.

e.args[1] is the right way to retrieve the message with MySQLdb: e.message is empty, and str(e) will display ‘(ERR CODE, “ERR MSG”)’.

Answered by cedbeu

Solution #5

To examine the error notice and take action in response to it (with Python 3)…

try:
    some_method()
except Exception as e:
    if {value} in e.args:
        {do something}

Answered by DanGoodrick

Post is based on https://stackoverflow.com/questions/4308182/getting-the-exception-value-in-python