If I have that code:
try:
some_method()
except Exception, e:
How can I get this Exception value (string representation I mean)?
For python2, It's better to use e.message
to get the exception message, this will avoid possible UnicodeDecodeError
. But yes e.message
will be empty for some kind of exceptions like OSError
, in which case we can add a exc_info=True
to our logging function to not miss the error.
For python3, I think it's safe to use str(e)
.