.NET defines the InvalidOperationException
as
The exception that is thrown when a method call is invalid for the
object’s current state.
In an optimal software design, would it ever make sense to throw this exception? I would argue that the ‘optimal’ API prevents any misuse, including, never allowing a method to be called when its state is invalid.
3
I generally agree with your statement, but when designing an API, this type of exception would be useful to a developer who is interfacing with that API. Whereby the developer would be informed by this exception that his/her code is structured incorrectly.
It would also be best practice to define a verbose message back with the exception indicating what the issue is in plain English (or whatever language is your primary).
6