[Python-Dev] Bare except clauses in PEP 348

Raymond Hettinger raymond.hettinger at verizon.net
Thu Aug 25 11:35:31 CEST 2005

> > OK, I'm convinced. Let's drop bare except for Python 3.0, and
> > deprecate them until then, without changing the meaning.
> >
> Woohoo

That's no cause for celebration.  Efforts to improve Py3.0 have spilled
over into breaking Py2.x code with no compensating benefits.  Bare
except clauses appear in almost every Python book that has ever been
written and occur at least once in most major Python applications.

I had thought the plan was to introduce Py3.0 capabilities into 2.x as
they become possible but not to break anything.  Isn't that why string
exceptions, buffer(), and repr() still live and breathe?

We don't have to wreck 2.x in order to make 3.0 better.  I wish the 3.0
PEPs would stop until we are actually working on the project and have
some chance of making people's lives better.  If people avoid 2.5 just
to avert unnecessary breakage, then Py3.0 doesn't benefit at all.

I propose that the transition plan be as simple as introducing
BaseException.  This allows people to write code that will work on both
2.x and 3.0.  It doesn't break anything.  

The guidance for cross-version (2.5 to 3.0) code would be:

* To catch all but terminating exceptions, write:

    except (KeyError, SystemExit):
    except Exception:               

* To catch all exceptions, write:
    except BaseException:           

To make the code also run on 2.4 and prior, add transition code:

    except NameError:
        class BaseException(Exception):

With that minimal guidance, people can write code that works on from 2.0
to 3.0 and not break anything that is currently working.  No
deprecations are necessary.

Remember, the ONLY benefit from the whole PEP is that in 3.0, it will no
longer be necessary to write "except (KeyError, SystemExit):  raise".
Steven and Jack's research show that that doesn't arise much in practice
anyway.  IOW, there's nothing worth inflicting destruction on tons of
2.x code.


More information about the Python-Dev mailing list