PEP 484 introduced type hints, at this time documenting exceptions was left to docstrings. I seek to suggest a reason this feature might be desirable along with how it might be used. Error handling in python does an excellent job of keeping the error-path out of the way when writing the normal flow of logic, however for larger code bases it is not always clear what exceptions may be caused by calling existing code. Since these cases are easily missed they may reach a higher level than intended ...
This is a discussion on Python’s forums about adding something akin to a throws keyword in python.
When I used to write Java and switched to Python, this was one of the things I missed. It was always quite clear which exceptions I had to catch (or not). Just today, I ran into the issue of trying to cover the exceptions a library could throw without using except: or except Exceptionas e, but finally gave up and gave in to it. The linter wasn’t happy, but fuck it.
It was always quite clear which exceptions I had to catch (or not)
Lol. You’re literally the only one that likes checked exceptions. And, it seems you think that it actually gives you information about what exceptions to catch. It does not. Most things just catch and rethrow as a RuntimException
Hey, I like checked exceptions too! I honestly think it’s one of Javas’s best features but it’s hindered by the fact that try-catch is so verbose, libraries aren’t always sensible about what exceptions they throw, and methods aren’t exception-polymorphic for stuff like the Stream API. Which is to say, checked exceptions are a pain but that’s the fault of the rest of the language around them and not the checked exceptions per se.
When I used to write Java and switched to Python, this was one of the things I missed. It was always quite clear which exceptions I had to catch (or not). Just today, I ran into the issue of trying to cover the exceptions a library could throw without using
except:
orexcept Exception as e
, but finally gave up and gave in to it. The linter wasn’t happy, but fuck it.@onlinepersona You just need pip install fuckit
https://pypi.org/project/fuckit/
Lol. You’re literally the only one that likes checked exceptions. And, it seems you think that it actually gives you information about what exceptions to catch. It does not. Most things just catch and rethrow as a RuntimException
Hey, I like checked exceptions too! I honestly think it’s one of Javas’s best features but it’s hindered by the fact that try-catch is so verbose, libraries aren’t always sensible about what exceptions they throw, and methods aren’t exception-polymorphic for stuff like the Stream API. Which is to say, checked exceptions are a pain but that’s the fault of the rest of the language around them and not the checked exceptions per se.
I also like checked exceptions. I like having a compile time check that I thought through error scenarios.
Is it perfect? No, but it should be iterated upon, not discarded.
FYI, catching and rethrowing as an unchecked exception is a pretty bad anti-patern (and a foul code smell).