Greg Ewing wrote: > > You can reduce the amount of boilerplate by doing something > like this: > class MySpecialException(Exception): > pass > def handle_my_special_exception(a, b, c, d, e, f): > .... > try: > if some_test_that_fails(variables): > raise MySpecialException(a, b, c, d, e, f) > except MySpecialException as e: > if not handle_my_special_exception(*e.args): > raise
Yeah, I could remove some of the argument assignment, but I think your example points out even more strongly how useless that class was in managing the exception. It is literally just a name that I am forced to define outside the flow of logic. If exceptions had less boilerplate they might get used more often with more description. It would be great to get a KeyError like this: try: if c[name] == 'fried chicken': eat(c[name]) except KeyError.missingKey(key): c[key] = getMoreChicken() A little contrived since the key name is available in the same scope, but the clarity of passing a variable along with the exception in a function like syntax makes the handler more obvious and the ease of throwing these named returns would likely lead to more exception handling instead of checking first then doing it. Even without the parameters, this on the fly exception definition would be useful and encourage code clarity. You could do things like: try: r = requests.get(some_url) except HTTPError.notFound(url): print(f"{url} not found. Try another one") except HTTPError: print(f"Unknown HTTP Error that doesn't obviously advertise its self! PANIC") raise Of course, again, looking at the requests code shows just how much boilerplate these things take up. The base class RequestException has some code in it but then there are 22 derived exceptions. So many classes defined just to give a name to an exception and none of them have special parameters guaranteed in the exception that might help you better handle what happened. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/KNGACZYDMKYYD7TDXRBE2LOZWQJHIWGK/ Code of Conduct: http://python.org/psf/codeofconduct/