Defining a new exception with multiple args

Erik Stephens samurai at ep.newtimes.com
Thu Jan 11 11:15:11 EST 2001


Daniel Klein wrote:
> 
> class RecordNotFoundException(Exception):
>     def __init__(self, filename, recordid):
>         self.filename = filename
>         self.recordid = recordid
> 
> Why is it showing '<unprintable instance object>' instead of the arguments I
> passed?

Judging from the source code, it should print an empty string.  I don't
know why '<unprintable instance object>' is coming back.  What version
of Python are you using?

>From Python 1.5.2 exceptions.py:

...

class Exception:
    """Proposed base class for all exceptions."""
    def __init__(self, *args):
        self.args = args

    def __str__(self):
        if not self.args:
            return ''
        elif len(self.args) == 1:
            return str(self.args[0])
        else:
            return str(self.args)

    def __getitem__(self, i):
        return self.args[i]

...

Try adding this to your __init__ method:

    Exception.__init__(self, filename, recordid)
    
-Erik



More information about the Python-list mailing list