[Python-ideas] Proposal: Abolition of bare except clauses
Chris Angelico
rosuav at gmail.com
Sat Apr 11 13:29:32 CEST 2015
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.
* 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.
Cons:
* May make it harder to write Py2/Py3-compatible code. In Py2,
"except:" will catch old-style-class exceptions.
* Any removal of any feature can cause examples and published code to break.
Undecided:
* Forcing everyone to look at the code that uses "except:" means extra
work to upgrade Python, but it might mean they notice a problem
somewhere. Maybe it should be "except Exception", or maybe something
even more specific. Is this a pro or a con?
Thoughts?
ChrisA
More information about the Python-ideas
mailing list