Let's say I have this
try:
#some code here
except Exception, e:
print e
print repr(e)
From this block of code I get
>>
>> <exceptions.Exception instance at 0x2aaaac3281b8>
Why don't I have any exception message and, moreover, what does second message means?
You have an exception which produces an empty str
(i.e. str(e)
is empty). Why that is cannot be known from the limited code you posted, you will have to look at the traceback to see where the exception came from.
As for repr()
, that's intended to produce a string which may be ugly which allows reconstruction of the object, rather than pretty-printing. It's not what you want for printing exceptions.
An Exception-derived object is thrown in # some code here
. This object has a __str__
method that returns an empty string or spaces and no __repr__
method.
See Python Help.
class SomeClass(object):
def __str__(self):
# Compute the 'informal' string representation of an object.
return 'Something useful'
def __repr__(self):
# Compute the 'official' string representation of an object. If at all possible, this should look like a valid
# Python expression that could be used to recreate an object with the same value (given an appropriate environment).
return 'SomeClass()'
o = SomeClass()
print o
print repr(o)
Outputs;
Something useful
SomeClass()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With