[Python-ideas] Proposal: Abolition of bare except clauses

Chris Angelico rosuav at gmail.com
Sat Apr 11 14:28:17 CEST 2015


On Sat, Apr 11, 2015 at 10:10 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
>>
>> Pros:
>> * Remove the attractive nuisance of "hmm, my code's throwing an error
>> that I don't recognize, I'll use try/except" and just catching
>> everything.
>
>
> This doesn't different from the use of "except BaseException:".

Apart from the fact that "except:" is short and convenient. If someone
were to quickly scribble something down, it's more likely to be
Exception than BaseException, which at very least would prevent the
catching of KeyboardInterrupt and friends; and since you're forced to
put _something_ in, there's a better chance that you'll go and put the
right thing in.

>> * Eliminates the edge case wherein "except:" and "except ():" are
>> almost diametrically opposite in meaning. Not often a concern, but
>> we've just had a lengthy thread on python-list discussing this. It's
>> generally agreed that an empty tuple has to be interpreted as catching
>> nothing, but the false parallel with an empty clause is problematic.
>
>
> Is this a problem?

Hardly a huge one, but it's certainly not the other way around.

>> Cons:
>> * May make it harder to write Py2/Py3-compatible code. In Py2,
>> "except:" will catch old-style-class exceptions.
>
>
> I think this is enough to not abolish bare except clauses at least while 2.7
> in the use.

Are old-style classes often thrown? And is there any advantage to not
just adding "(Exception)" to the classes' definitions? Currently, the
bare except clause is even weirder in 2.7, because there are now three
distinct levels of "catch everything":

# what most people should think of as "everything"
except Exception as e:
# what the exception hierarchy sees as "everything"
except BaseException as e:
# absolutely everything
except:

Plus, the bare except clause doesn't allow capturing. One good use of
"catch everything" is the generic logger at a significant boundary
(maybe a web server that runs application code; if the app chokes, the
server should catch that, log it, and return a 500), and this is nice
and tidy with "except BaseException as e", but with "except:", you
have to go digging. It's in many ways a wart; what benefit is it,
really?

ChrisA


More information about the Python-ideas mailing list