[Python-Dev] Bare except clauses in PEP 348
Raymond Hettinger
raymond.hettinger at verizon.net
Wed Aug 24 04:41:01 CEST 2005
[Guido van Rossum]
> If we syntactically enforce that the bare except, if present, must be
> last, would that remove your objection? I agree that a bare except in
> the middle is an anomaly, but that doesn't mean we can't keep bare
> except: as a shorthand for except Exception:.
Hmm. Prohibiting mid-suite bare excepts is progress and eliminates the
case that causes immediate indigestion.
As for the rest, I'm not as sure and it would be helpful to get thoughts
from others on this one. My sense is that blocking the clause from
appearing in the middle is treating the symptom and not the disease.
The motivating case for the most of the PEP was that folks were writing
bare except clauses and trapping more than they should. Much of the
concern was dealt with just by giving a choice between writing Exception
and BareException depending on the intended result.
That leaves the question of the default value a bare except with
Exception being the most useful and BaseException being the most
obvious.
While I don't doubt that Exception is the more useful, we have already
introduced a new builtin and moved two other exceptions to meet this
need. Going further and altering the meaning of bare except seems like
overkill for a relatively small issue.
My remaining concern is about obviousness. How much code has been
written or will be written that intends a bare except to mean
BaseException instead of Exception. Would such code erroneously pass a
code review or inspection. I suspect it would. The code looks like
does one thing but actually does something else. This may or may not be
a big deal.
Raymond
More information about the Python-Dev
mailing list