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

Serhiy Storchaka storchaka at gmail.com
Sat Apr 11 14:10:03 CEST 2015


On 11.04.15 14:29, Chris Angelico wrote:
> A bare "except:" clause catches any exception. As of Python 3.0, this
> can be spelled more explicitly as "except BaseException:", and AFAIK
> the only difference between the two is that someone might rebind
> BaseException.
>
> String exceptions were abolished during the 2.x line, without causing
> major upheaval. The deprecation and ultimate abolition of the bare
> except syntax would have less consequence than that, as it would
> merely require a mechanical transformation to "except BaseException:",
> without needing any corresponding changes at raise site.
>
> 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:".

> * 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?

> 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.




More information about the Python-ideas mailing list