Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top