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?
Answer from aaronasterling on Stack Overflowuse 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?
Use repr() and The difference between using repr and str
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
In Python 3.x, str(e) should be able to convert any Exception to a string, even if it contains Unicode characters.
So unless your exception actually returns an UTF-8 encoded byte array in its custom __str__() method, str(e, 'utf-8') will not work as expected (it would try to interpret a 16bit Unicode character string in RAM as an UTF-8 encoded byte array ...)
My guess is that your problem isn't str() but the print() (i.e. the step which converts the Python Unicode string into something that gets dumped on your console). See this answer for solutions: Python, Unicode, and the Windows console
Try this, it should work.
try:
raise Exception('X')
except Exception as e:
print("Error {0}".format(str(e.args[0])).encode("utf-8"))
Considering you have only a message in your internal tuple.
In my experience what you want is repr(err), which will return both the exception type and the message.
str(err) only gives the message.
From the docs for print()
All non-keyword arguments are converted to strings like str() does and written to the stream
So in the first case, your error is converted to a string by the print built-in, whereas no such conversion takes place when you just try and concatenate your error to a string. So, to replicate the behavior of passing the message and the error as separate arguments, you must convert your error to a string with str().
try:
#some deliberately dodgy code possibly involving pig's head (ask dodgy Dave)
except Exception as exc:
print(exc)
exc is not a string (it's an exception object) but print will call its __str__ method which will return the message string with which it was instantiated
(you don't need to call it exc, you can call it anything you like, but exc or e are quite commonly used)
If you want the message in a variable you can always do explicitly
message = str(exc) # now it really is a string
inside the except block
Exception is a class, you should make an object inherited from Exception, like this:
try:
#some deliberately dodgy code
Except Exception e:
print(e)