Reputation: 16479
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.
Upvotes: 1
Views: 8666
Reputation: 1017
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()
Upvotes: 2
Reputation: 34166
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.
Upvotes: 4