Jean Abou Samra wrote: > Le 01/09/2022 à 23:40, Steve Jorgensen a écrit : > > I frequently find that I want to raise an exception when the target of a > > call is not in an appropriate state to perform the requested operation. > > Rather than choosing between `Exception` or defining a custom exception, it > > would be nice if there were a built-in `InvalidStateError` exception that > > my code could raise. > > In cases where I want to define a custom exception anyway, I think it would > > be nice if it could have a generic `InvalidStateError` exception class for > > it to inherit from. > > Of course, I would be open to other ideas for what the name of this > > exception should be. Other possibilities off the top of my head are > > `BadStateError` or `StateError`. > > https://docs.python.org/3/library/exceptions.html#ValueError states that > ValueError is “Raised when an operation or function receives an argument > that has the right type but an inappropriate value, and the situation is > not described by a more precise exception such as |IndexError| > https://docs.python.org/3/library/exceptions.html#IndexError.” How would > a "state error" differ from this more precisely? What value would this new > exception type add? Both ValueError and this proposed StateError are very > generic.
`ValueError` is about for when the value of an argument passed to the function is unacceptable. The exception that I propose would be for when there is nothing wrong with any argument value, but the object is not in the correct state for that method to be called. I should have provided an example. One example is when trying to call methods to interact with a remote system either before a connection has been made or after the connection has been terminated. _______________________________________________ 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/W2G5XNWKS6KHXSCH45QPLFRUMZIVNS4L/ Code of Conduct: http://python.org/psf/codeofconduct/