[Tutor] class questions

Eike Welk eike.welk at gmx.net
Sat Jun 26 22:41:59 CEST 2010


Hello Payal!

On Saturday June 26 2010 19:05:16 Payal wrote:
> Can we say that our own exception classes have only maybe a doc-string
> and pass, nothing more?

No, you let the exception transport the information that you need for handling 
the error. This is an exception that I use to transport user visible error 
messages in a compiler, that I write as a hobby:


class UserException(Exception):
    '''Exception that transports user visible error messages.'''
    def __init__(self, message, loc=None, errno=None):
        Exception.__init__(self)
        self.msg = message
        self.loc = loc
        self.errno = errno

    def __str__(self):
        if self.errno is None:
            num_str = ''
        else:
            num_str = '(#%s) ' % str(self.errno) 
        return 'Error! ' + num_str + self.msg + '\n' + str(self.loc) + '\n'

    def __repr__(self):
        return self.__class__.__name__ + str((self.msg, self.loc, 
                                              self.errno))

It contains:
    self.msg : The error message
    self.loc : An object encoding the location of the error in the program's
               text. Together with the file name and the text.
    self.errno : An integer to identify the error, for the test framework.

That said; the expression's type and the error message are often sufficient to 
handle the error.


Eike.


More information about the Tutor mailing list