On Tue, Oct 13, 2020 at 12:47:45PM -0400, David Mertz wrote:
> On Tue, Oct 13, 2020 at 6:18 AM Steven D'Aprano <[email protected]> wrote:
>
> > I don't think that a two line class (perhaps a couple of extra
> > lines if you give it a docstring) justifies the name "boilerplate":
> >
> > class MySpecialException(Exception):
> > pass
> >
>
> I think that in 22 years of using Python, I have never written an exception
> that took more than these two lines of code.
I have, once or twice in a blue moon, written more complex exceptions,
but few of them survived long enough to make it into actual working
code.
> Heck, I even have my memory jogged of string exceptions reading this. When
> did those go away fully, 1.5.2? 2.1?
String exceptions hung around a lot longer than people think, they were
first deprecated in 2.5 and not removed for good until 2.6.
$ python2.5 -c "raise 'spam and eggs'"
-c:1: DeprecationWarning: raising a string exception is deprecated
Traceback (most recent call last):
File "<string>", line 1, in <module>
spam and eggs
> I DID in the discussion, immediately think of making an exception a
> dataclass, as someone else replied with. I guess if you want cargo in your
> exception, that's a pretty good way to do it.
"Cargo", that's an excellent term, thank you.
> But really the ONLY thing I
> ever want in an exception is an inheritance tree. An exception feels like
> a really unnatural way to pass around data
Indeed. Very few exceptions in the std lib are used that way, which
hints that it may not be a great idiom, or at least not a great cultural
fit.
As Wes pointed out, catching exceptions is relatively slow, and using
exceptions for flow control makes them a glorified GOTO with all the
risks that entails. Or even COMEFROM. Urggh.
I don't entirely agree with this article:
http://www.lighterra.com/papers/exceptionsharmful/
but the points it makes are important. See also:
https://stackoverflow.com/questions/1736146/why-is-exception-handling-bad
https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why
So I'm cautious about using exceptions for complex flow control.
(Iteration and StopIteration is a very simple example of flow control.)
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/MM7V6ELTE6AF63HKKL2L7DW2O4GMVQG7/
Code of Conduct: http://python.org/psf/codeofconduct/