<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Apr 11, 2015, at 04:29, Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><span>A bare "except:" clause catches any exception. As of Python 3.0, this</span><br><span>can be spelled more explicitly as "except BaseException:", and AFAIK</span><br><span>the only difference between the two is that someone might rebind</span><br><span>BaseException.</span><br><span></span><br><span>String exceptions were abolished during the 2.x line, without causing</span><br><span>major upheaval. </span></div></blockquote><div><br></div><div>I couldn't remember when this happened, so I look at the docs... But 2.7 (<a href="https://docs.python.org/2.7/reference/executionmodel.html#exceptions">https://docs.python.org/2.7/reference/executionmodel.html#exceptions</a>) still say "<span style="background-color: rgba(255, 255, 255, 0);"><span style="text-align: justify;">Exceptions can also be identified by strings, in which case the </span><a class="reference internal" href="https://docs.python.org/2.7/reference/compound_stmts.html#except" style="text-decoration: none; text-align: justify;"><tt class="xref std std-keyword docutils literal" style="padding: 0px 1px; font-weight: bold;"><span class="pre">except</span></tt></a><span style="text-align: justify;"> clause is selected by object identity." Do the docs need to be fixed?</span></span></div><div><span style="text-align: justify; background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="text-align: justify; background-color: rgba(255, 255, 255, 0);">Meanwhile, in the C API (</span><a href="https://docs.python.org/2.7/c-api/exceptions.html#string-exceptions">https://docs.python.org/2.7/c-api/exceptions.html#string-exceptions</a>), it says "<span class="versionmodified" style="background-color: rgba(255, 255, 255, 0); font-style: italic;">Changed in version 2.6: </span><span style="background-color: rgba(255, 255, 255, 0);">All exceptions to be raised or caught must be derived from</span><span style="background-color: rgba(255, 255, 255, 0);"> </span><a class="reference internal" href="https://docs.python.org/2.7/library/exceptions.html#exceptions.BaseException" title="exceptions.BaseException" style="background-color: rgba(255, 255, 255, 0); text-decoration: none;"><tt class="xref py py-exc docutils literal" style="padding: 0px 1px; font-weight: bold;"><span class="pre">BaseException</span></tt></a><span style="background-color: rgba(255, 255, 255, 0);">.</span><span style="background-color: rgba(255, 255, 255, 0);">" I think that one is correct, but until I sit down at a bigger screen where I can browse the source or run an interpreter, I'm not sure.</span></div><br><blockquote type="cite"><div><span>The deprecation and ultimate abolition of the bare</span><br><span>except syntax would have less consequence than that, as it would</span><br><span>merely require a mechanical transformation to "except BaseException:",</span><br><span>without needing any corresponding changes at raise site.</span><br><span></span><br><span>Pros:</span><br><span>* Remove the attractive nuisance of "hmm, my code's throwing an error</span><br><span>that I don't recognize, I'll use try/except" and just catching</span><br><span>everything.</span><br></div></blockquote><div><br></div><div>But if you just teach people "spell that as except BaseException:" (or, more likely, "... as except Exception:"), it's still the exact same attractive nuisance. And, because "except:" is almost always a red flag in novice code, but "except Exception:" is occasionally reasonable, you're making it a tiny bit harder to spot the mistake when helping novices, without actually fixing it.</div><div><br></div><div>(Of course requiring an "as" clause would solve all of this, but nobody wants that...)</div><br><blockquote type="cite"><div><span></span><span>* Eliminates the edge case wherein "except:" and "except ():" are</span><br><span>almost diametrically opposite in meaning. Not often a concern, but</span><br><span>we've just had a lengthy thread on python-list discussing this. It's</span><br><span>generally agreed that an empty tuple has to be interpreted as catching</span><br><span>nothing, but the false parallel with an empty clause is problematic.</span><br><span></span><br><span>Cons:</span><br><span>* May make it harder to write Py2/Py3-compatible code. In Py2,</span><br><span>"except:" will catch old-style-class exceptions.</span><br></div></blockquote><div><br></div><div>If the C API docs are right and exceptions must be derived from</div><div>BaseException even in 2.6, they can't be old-style class instances in the first place. (And if anyone's trying to write 2.5/3.x code, they still have to deal with string exceptions, not to mention that they can't get the value of any exceptions they handle, so they've got much bigger problems.)</div><div><br></div><div>Even if that's not right, what problems do old-style classes cause that new-style-but-not-BaseException-derived classes don't?</div><br><blockquote type="cite"><div><span></span><span>* Any removal of any feature can cause examples and published code to break.</span><br><span></span><br><span>Undecided:</span><br><span>* Forcing everyone to look at the code that uses "except:" means extra</span><br><span>work to upgrade Python, but it might mean they notice a problem</span><br><span>somewhere. Maybe it should be "except Exception", or maybe something</span><br><span>even more specific. Is this a pro or a con?</span><br><span></span><br><span>Thoughts?</span><br><span></span><br><span>ChrisA</span><br><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a></span><br><span>Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a></span><br></div></blockquote></body></html>