On Wed, Jan 7, 2009 at 11:09 AM, Chris Rebert <c...@rebertia.com> wrote:
> On Tue, Jan 6, 2009 at 6:56 PM, Steven Woody <narkewo...@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.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to