Getting the exception value in Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

If I have that code:

try:
    some_method()
except Exception,e:

How can I get this Exception value (string representation I mean)?

Thanks

Answers

use str

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

Also, most exception classes will have an args attribute. Often, args[0] will be an error message.

It should be noted that just using str will return an empty string if there s no error message whereas using repr as pyfunc recommends will at least display the class of the exception. My take is that if you re printing it out, it s for an end user that doesn t care what the class is and just wants an error message.

It really depends on the class of exception that you are dealing with and how it is instantiated. Did you have something in particular in mind?

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/4308182/getting-the-exception-value-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils