[Python-Dev] FW: Bare except clauses in PEP 348

Michael Chermside mcherm at mcherm.com
Wed Aug 24 18:33:00 CEST 2005


Raymond writes:
> Hey guys, don't give up your bare except clauses so easily.
      [...]

Raymond:

I agree that when comparing:

   // Code snippet A
   try:
       ...
   except SomeException:
       ...
   except BaseException:
       ...

with

   // Code snippet B
   try:
       ...
   except SomeException:
       ...
   except:
       ...

that B is nicer than A. Slightly nicer. It's a minor esthetic point. But
consider these:

    // Code snippet C
    try:
        ...
    except Exception:
        ...

    // Code snippet D
    try:
        ...
    except:
        ...

Esthetically I'd say that D is nicer than A for the same reasons. It's a minor
esthetic point. But you see, this case is different. You and I would likely
never bother to compare C and D because they do different things! (D is
equivalent to catching BaseException, not Exception). But we know that people
who are not so careful or not so knowlegable WILL make this mistake... they
make it all the time today!

Since situation C (catching an exception) is hundreds of times more common than
situation A (needing default processing for exceptions that don't get caught,
but doing it with try-except instead of try-finally because the
nothing-was-thrown case is different), I would FAR rather protect beginners
from erroniously confusing C and D than I would provide a marginally more
elegent syntax for the experts using A or B. And that elegence is arguable...
there's something to be said for simplicity, and having only one kind of
"except" clause for try statements is clearly simpler than having both "except
<some-exception-type>:" and also bare "except:".

-- Michael Chermside



More information about the Python-Dev mailing list