Exception Messages

Dave Angel d at davea.name
Mon Oct 15 12:42:48 EDT 2012


On 10/15/2012 12:34 PM, MRAB wrote:
> On 2012-10-15 17:22, John Gordon wrote:
>> In <mailman.2217.1350317845.27098.python-list at python.org> MRAB
>> <python at mrabarnett.plus.com> writes:
>>
>>> > Why wasn't the message printed out?
>>>
>>> You didn't add a __str__ method:
>>
>>> class PvCamError(Exception):
>>>      def __init__(self, msg):
>>>          self.msg = msg
>>>      def __str__(self):
>>>          return self.msg
>>
>> Wouldn't PvCamError inherit __str__() from Exception?
>>
> Yes, but you've put the message in msg, and Exception doesn't have that
> attribute.
>
> An alternative approach would be:
>
> class PvCamError(Exception):
>     pass
>

Wouldn't another approach be for this __init__() method to call the
base-class __init__() method with whatever data it needs?



-- 

DaveA




More information about the Python-list mailing list