Java 如何返回队列为空错误?

Java 如何返回队列为空错误?,java,Java,如果队列为空,如何抛出错误 public T dequeue() { T o = null; if (head != null) { o = head.getData(); head = head.getNext(); queueSize--; } return o; 如果a为空,则不存在头节点。因此,在检查head是否为null的对面创建一个else子句

如果队列为空,如何抛出错误

public T dequeue() {
        T o = null;
        if (head != null) {
            o = head.getData();
            head = head.getNext();
            queueSize--;
        }
        return o;
如果a为空,则不存在头节点。因此,在检查head是否为null的对面创建一个else子句

 public T dequeue() {
    T o = null;
    if (head != null) {
        o = head.getData();
        head = head.getNext();
        queueSize--;
    }
    // Otherwise, the head is null
    else{
       // Throw exception
       throw new NoSuchElementException()
    }
    return o;

throw new NoSuchElementException()
如果
head==null

否则{throw new SomeException(“…”);}
但是说真的,如果你在来这里之前查过这个,你就已经知道了,不是吗?