I am practicing Junit testing and I created a small stack class that uses ArrayList as the stack.

My pop() method:

public void pop() {
    if (count > 0) {
        stack.remove(0);
        count--;
    }
    else {

    }
}

I am not sure what to do if the stack is empty. I want to throw an exception but I'm not sure which one to throw.

有帮助吗?

解决方案

You can throw an EmptyStackException:

throw new EmptyStackException();

Edit:

In this page from Java Docs, there is an example of the pop() method and the exception mentioned before.

其他提示

What about doing what the standard Java "Stack#pop" does in this case? See http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html#pop()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top