I thought of a third advantage to this approach: Except clauses will be easier to understand. If I'm reading someone's code and I see `except ValueError`, I'm gonna have to do a bit of thinking to figure out what exactly they're catching. On the other hand, if the code is `except IntParsingError`, then I know exactly what it is they're catching. On Fri, May 1, 2020 at 9:48 AM Ram Rachum <ram@rachum.com> wrote:
Hi,
Here's something I wanted in Python for many years. If this has been discussed in the past, please refer me to that discussion.
On one hand, it's something that I can't imagine the python-dev community supporting. On the other hand, it would maintain backward compatibility.
I wish there were a 100 more built-in exceptions in Python, that will be very specific about what went wrong.
If I do this:
>>> x, y = range(3)
I know it'll raise a ValueError, because I've memorized that, but it did take me a few years to remember where I should expect ValueError and where I should expect TypeError.
It would be nice if the operation above raised UnpackingOverflowError, which will be a subclass of UnpackingError, along with UnpackingUnderflowError. UnpackingError can be a subclass of ValueError, for backward compatibility.
Similarly, if I did this:
>>> def f(x, y): return x + y >>> f(1)
I would get a TypeError. Would be a lot cooler if I got MissingArgumentsError, which would be a subclass of SignatureError, which would be a subclass of TypeError.
There are 2 reasons I want this:
1. When I'm writing a try..except clause, I want to catch a specific exception like MissingArgumentsError rather than ValueError or TypeError. They're too ubiquitous. I don't want some other unexpected failure producing the same ValueError and triggering my except clause.
2. When I get an error, especially from some shitty corporate system that truncates the traceback, I want to get as many hints as possible about what went wrong.
It's true that today, most Python exceptions have good text in their message, like "TypeError: f() missing 1 required positional argument: 'y'". But that isn't guaranteed everywhere, and specific exception types could help.
What do you think?
Ram.