Of course, there are other ways of writing this code, but imagine this for a database interface where a save normally returns the saved object (inspired by Django) ``` try: x, y = Foo.save() except ValueUnpackingError: // oh... this means saving failed (it returned None instead) // let's return a sensible response return ExceptionToResponse(FooStateToException(foo.state)) ``` Another example would be an interactive session where you expect an iterable to have 3 entries (say for a 3D space coordinate) and you want to check this specifically: ``` try: x, y, z = point except ValueUnpackingError: print(f"point has wrong dimension") ``` where point is an instance of: ``` class Point: def __iter__(self): return (float(x) for x in self._internal) ``` Can you spot here why ValueError would result in significantly different behaviour? ValueError will also catch it if point's internal list contains a bad entry (e.g. a string that does not convert to a float). Having these exceptions does not take anything away as they can choose the more general version. On Sat, 2 May 2020 at 11:12, Eric V. Smith <eric@trueblade.com> wrote:
On 5/2/2020 6:00 AM, Henk-Jaap Wagenaar wrote:
On Fri, 1 May 2020 at 10:29, Steven D'Aprano <steve@pearwood.info> wrote: [...]
"Is it UnpackingOverflowException or PackingUnderflowError or TooManyValuesUnpackException or ValueUnpackingError or ...???"
It took me far too long to learn the difference between UnicodeEncodingError and UnicodeDecodingError. Or is it UnicodeEncodeError and UnicodeDecodeError?
It is possible to have too many fine-grained errors as well as too few.
This is thinking about exceptions and the proposal too simply. When I read this, I (a) found out that this exception is a built-in so it isn't hard to find out which is which (any IDE or IPython would help) but more importantly, looking at their MRO, if you cannot understand the difference, and you don't have to, you could instead choose to catch UnicodeError instead (or the combination of both).
However, if you just want to catch encoding errors you can, and this is the crux of this proposal: allowing similar catches throughout the standard library as a first class citizen seems sensible to me and I am +1 of this proposal.
Please show an example of where you would catch ValueUnpackingError and how you'd handle it (recover, re-raise, whatever).
Eric
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/I26BBA... Code of Conduct: http://python.org/psf/codeofconduct/