[concurrency-interest] LinkedBlockingQueue does not throwClassCastException

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Tue Apr 17 04:23:20 EDT 2007


On 17/04/07, David Holmes <dcholmes at optusnet.com.au> wrote:
> If you check the documentation ...

That is what I did.

> ... you will see that LinkedBlockingQueue.add is
> inherited from AbstractQueue.add, and the latter does *not* throw
> ClassCastException.

On the contrary to what you claim, this is what I found:

http://java.sun.com/javase/6/docs/api/java/util/AbstractQueue.html#add(E)

add

public boolean add(E e)

    Inserts the specified element into this queue if it is possible to
do so immediately without violating capacity restrictions, returning
true upon success and throwing an IllegalStateException if no space is
currently available.

    This implementation returns true if offer succeeds, else throws an
IllegalStateException.

    Specified by:
        add in interface Collection<E>
    Specified by:
        add in interface Queue<E>
    Overrides:
        add in class AbstractCollection<E>

    Parameters:
        e - the element to add
    Returns:
        true (as specified by Collection.add(E))
    Throws:
        IllegalStateException - if the element cannot be added at this
time due to capacity restrictions
        ClassCastException - if the class of the specified element
prevents it from being added to this queue
        NullPointerException - if the specified element is null and
this queue does not permit null elements
        IllegalArgumentException - if some property of this element
prevents it from being added to this queue

End of doc

Now, what I can see from the above document is that the inherited
method can throw four types of exceptions and among them is the
ClassCastException. Why do you claim that `AbstractQueue.add, and the
latter does *not* throw
ClassCastException'? Is is not the right documentation? If this is the
correct documentation, how can one reproduce a ClassCastException?
That was all I wanted to know all along.

Besides, I would be interested in creating an IllegalArgumentException as well.

Best Regards,
Szabolcs


More information about the Concurrency-interest mailing list