[concurrency-interest] Exception throw but not speficied in the method contract of its interface

David Holmes dholmes@dltech.com.au
Mon, 11 Oct 2004 08:49:59 +1000

> Jean Morissette writes:
> the method add(Object) of class AbstractQueue can throw an
> IllegalStateException that is not specified in the contract of its
> interface Collection.  Is-it normal?

If you look at the specification for Collection.add it states:

"If a collection refuses to add a particular element for any reason other
than that it already contains the element, it must throw an exception
(rather than returning false). "

And this is exactly what AbstractQueue does - and what AbstractQueue
documents that it does.

While unchecked exceptions can always be thrown by an implementation, that
doesn't give an implementation the right to throw any unchecked exception
under any circumstance - proper subtyping requires adherence to the contract
of the supertype. But in this case the supertype explicitly allows for an
exception to be thrown, it just doesn't specify exactly what kind.

David Holmes