this question has answer here:
i missed built-in exception type in c# indicate object corrupted. throw in such cases?
usually miss when realize method, supposed work on object, fail if object had state. in such situations suspect state won't ever reached. being defensive it, i'd throw exception in case (e.g. after future code change).
for method arguments have argumentexception
can deny invalid parameters. object state? in java i'd use illegalstateexception
.
of course argue methods, changing state, check state correctness. , better should, if don't (say in legacy god classes)?
edit:
although invalidoperationexception
seems best fit, accepted answer states (and this one), please note:
it subtle, semantically has different meaning invalidoperationexception
. invalidoperationexception
indicates problem in "protocol" of object, caller has obey (e.g. not initialized, closed already, ...). in case caller did nothing wrong, it's object broken. transport message.
example:
switch(this._sometype) { case sometype.a: dosomething(); break; case sometype.b: dosomethingelse(); break; /*...*/ default: // unexpected type! introduced new type , didn't update this. throw new illegalstateexception("unknown type "+this._sometype); }
you should throw invalidoperationexception
indicate object has invalid state.
from msdn documentation (linked above):
the exception thrown when method call invalid object's current state.
Comments
Post a Comment