On Thu, Sep 01, 2022 at 09:40:05PM -0000, Steve Jorgensen wrote:

> 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.

If the target of the call isn't in an appropriate state, isn't that a 
bug in the constructor that it allows you to construct objects that are 
in an invalid state?

You should fix the object so that it is never in an invalid state rather 
than blaming the caller.

I believe that the interpreter may sometimes raise RuntimeError for 
cases where objects are in a broken internal state, but I've never seen 
one in real life. Oh, no, I was thinking of SystemError. Nevertheless, 
for my own classes, I've sometimes used RuntimeError for broken internal 
state. I have always considered that a bug in my code.

> 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.

What functionality do you expect this InvalidStateError superclass to 
provide that isn't already provided by Exception?


-- 
Steve
_______________________________________________
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/IZ7MFY36GYPMW5XPCUHZRRUB6GMK26EH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to