Does Java – IllegalStateException apply to immutable objects?

You throw an IllegalStateException if:

>The method cannot complete its work because of the value of one or more fields > which fields are final and only allocated in the constructor?

Textbook example: your class is an immutable collection < BigInteger > and your method should return the largest element, but this instance is empty

I've read Kevin bourillon ` s blog post on this topic, and I'm not sure which rule applies

Of course not Many instances of this class are not empty and the operation will succeed

Incomplete This instance is constructed with zero length, so this instance is not and will never be non empty

If the relevant parameter is implicit, this parameter can be applied This is an exception I'd love to make, but I'm afraid it may be confusing

Update: sample collection < BigInteger > has been changed from collection < integer > because the fact that there is an identity element (integer. Min_value) is distracting

Solution

I think the IllegalStateException is appropriate here If the construct is correct (that is, the "it's too late" section), the instance may be in the correct state

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>