can error messages be improved or can they be overridden ?

Chris cwitts at gmail.com
Mon Feb 23 08:51:44 EST 2009


On Feb 23, 11:57 am, Stef Mientki <stef.mien... at gmail.com> wrote:
> thanks Ron,
>
> but I was looking for a more general solution,
> in which I don't change the program itself,
> and where the error messages (in general) become more informative than
> it is by default.
>
> cheers,
> Stef
>
> Barak, Ron wrote:
> > Hi Stef,
>
> > You can do something like (not tested):
>
> > try:
> >    self.Brick.Par [ self.EP[2] ]['FileName'] = filename
> > except IndexError,e:
> >                 msg = "%s: '%s %s %s %d" % (e.strerror,e.filename,self.EP,self.EP[2],len(self.Brick.Par))
> >                 print msg
>
> > Bye,
> > Ron.
>
> >> -----Original Message-----
> >> From: Stef Mientki [mailto:stef.mien... at gmail.com]
> >> Sent: Sunday, February 22, 2009 15:43
> >> To: python-l... at python.org
> >> Subject: can error messages be improved or can they be overridden ?
>
> >> hello,
>
> >> I often get an error message like this
>
> >>     self.Brick.Par [ self.EP[2] ]['FileName'] = filename
> >> IndexError: list index out of range
>
> >> Now it would be very welcome,
> >> if the error message specified which index is out of range,
> >> in this case e.g.,
> >> - specifying the length of self.EP
> >> - specifying the value of self.EP[2]
> >> - specifying the length of self.Brick.Par etc..
>
> >> Is there a way to override the error message and provide this
> >> information, or is it impossible ?
>
> >> And if it's possible, am I the only one who often stumbles
> >> about this problem ?
> >> (I expect many people must have bounced in this problem before me ;-)
>
> >> thanks,
> >> Stef Mientki
>
>

Why not something like this ?  I know it requires changing your
application
but tbh I don't see any other way.

try:
    self.Brick.Par [ self.EP[2] ]['FileName'] = filename
except IndexError, msg:
    try:
        v = self.EP[2]
        try:
            v = self.Brick.Par[v]
        except IndexError:
            v = 'self.Brick.Par[%s] is out of range'%v
    except IndexError:
        v = 'self.EP[2] is out of range'
    raise IndexError(msg.message, v)



More information about the Python-list mailing list