message of Exception

Steven Woody narkewoody at gmail.com
Tue Jan 6 22:55:56 EST 2009


On Wed, Jan 7, 2009 at 11:09 AM, Chris Rebert <clp2 at rebertia.com> wrote:
> On Tue, Jan 6, 2009 at 6:56 PM, Steven Woody <narkewoody at gmail.com> wrote:
>> Hi,
>>
>> I am trying define an Exception as below:
>>
>> class MyError(Exception):
>>    def __init__(self, message):
>>        self.message = message
>>
>> And, I expect that when I raise a MyError as
>>    raise MyError, "my message"
>> the python should print a line such as
>>   MyError: my message
>>
>> But I did not get that, I just got:
>>   ...
>>   MyError
>>
>> I guess I had made something wrong with the __init__() definition, but
>> I can not find an answer in doc.  Please help, thanks!
>
> You need to call the superclass constructor:
>
> #note: this code will be different in Py 3.0
> class MyError(Exception):
>    def __init__(self, message):
>        super(MyError, self).__init__(message)

Thank you, I forgot to call base class's __init__()

>
> Or if you're not going to add anything else to MyError, you might as
> well just leave the class body empty and it'll use Exception's
> __init__, which takes care of the message.

Yes,  I know that, I just want to try and learn something.



More information about the Python-list mailing list