[PATCH] convert raise "..." to raise Exception ("...")

Matt Mackall mpm at selenic.com
Mon Aug 22 23:55:37 CDT 2005


On Mon, Aug 22, 2005 at 10:48:31PM -0400, Bart Trojanowski wrote:
> * Matt Mackall <mpm at selenic.com> [050822 21:25]:
> > If you change revlog.py:35 to:
> > 
> >     raise Exception("unknown compression type %s" % t)
> > 
> > ..we'll see which file and revision broken. Then send me a copy and
> > I'll try to dissect it. It's probably a single bit error in the index,
> > so be sure to send both files.
> 
> I don't understand python all that well, and I don't know if raising a
> string is ever ok.  The patch below may be complete garbage.

Raising a string is less than ideal because it's not part of the
normal Exception hierarchy. So it's legal, but not standard practice.

But just raising an Exception is suboptimal as well. It's better to at
least create a wrapper exception class like this:

 class RevlogError(Exception): pass

so that code higher up can filter in some fashion.

On the other hand, it's not good to design all this stuff until you
know what sort of errors you actually have.

> diff -r f859e9cba1b9 mercurial/hgweb.py
> --- a/mercurial/hgweb.py	Mon Aug 22 08:22:29 2005
> +++ b/mercurial/hgweb.py	Mon Aug 22 22:43:25 2005
> @@ -81,7 +81,7 @@
>                  if m:
>                      self.map[m.group(1)] = os.path.join(self.base, m.group(2))
>                  else:
> -                    raise "unknown map entry '%s'"  % l
> +                    raise Exception("unknown map entry '%s'" % l)
>  
>      def __call__(self, t, **map):
>          m = self.defaults.copy()

This is a singleton, so perhaps ought to be mapped onto LookupError.

> diff -r f859e9cba1b9 mercurial/revlog.py
> --- a/mercurial/revlog.py	Mon Aug 22 08:22:29 2005
> +++ b/mercurial/revlog.py	Mon Aug 22 22:43:25 2005
> @@ -32,7 +32,7 @@
>      if t == '\0': return bin
>      if t == 'x': return zlib.decompress(bin)
>      if t == 'u': return bin[1:]
> -    raise "unknown compression type %s" % t
> +    raise Exception("unknown compression type %s" % t)
...

These should all change to RevlogError, I think.
  
> --- a/mercurial/transaction.py	Mon Aug 22 08:22:29 2005
> +++ b/mercurial/transaction.py	Mon Aug 22 22:43:25 2005
> @@ -20,7 +20,7 @@
>  
>          # abort here if the journal already exists
>          if os.path.exists(journal):
> -            raise "journal already exists - run hg recover"
> +            raise Exception("journal already exists - run hg recover")
>  
>          self.report = report
>          self.opener = opener

And this is also a singleton, let's map it onto AssertionError.

-- 
Mathematics is the supreme nostalgia of our time.


More information about the Mercurial mailing list