PEP 317: Eliminate Implicit Exception Instantiation

Steven Taschuk staschuk at telusplanet.net
Mon Jun 9 18:02:55 EDT 2003


Quoth Bernhard Herzog:
  [solicited example of use of traceback argument to raise]
>         except SketchLoadError, value:
> 	    raise SketchLoadError('%d:%s' % (self.lineno, str(value))), None,\
>                   sys.exc_traceback
> 
> So, basically it's there to put some more information into the error
> message. Not all places where SketchLoadError is raised have access to
> the line number so it can't easily be done in other ways.

Yes, this makes sense.  It seems to me a bit odd to create a new
exception object, though; would it work to mutate the existing
SketchLoadError and re-raise it?

    class SketchLoadError(Exception):
        def __init__(self, msg, lineno=None):
            Exception.__init__(self, msg, lineno)
            self.msg = msg
            self.lineno = lineno
        def __str__(self):
            if self.lineno is None:
                return str(self.msg)
            else:
                return '%s:%s' % (self.lineno, self.msg)

    try:
        # ...
    except SketchLoadError, e:
        e.lineno = lineno
        raise

(I admit, of course, that this is a nontrivial change.  I'm just
trying to get a handle on the use case.)

  [...]
> threads so it shouldn't be a problem unless somebody now goes and starts
> deprecating sys.exc_traceback ;-)

It's been deprecated since 1.5 according to the Library Reference.
<wink>

-- 
Steven Taschuk                  staschuk at telusplanet.net
"Telekinesis would be worth patenting."  -- James Gleick





More information about the Python-list mailing list