The throw keyword is used to actually throw an exception while the throws keyword just tells the compiler which exceptions may be thrown by a method.
- Code: Select all
public Object pop() throws EmptyStackException
{
Object obj;
if (size == 0) {
throw new EmptyStackException();
}
obj = objectAt(size - 1);
setObjectAt(size - 1, null);
size--;
return obj;
}
See haw throws appears in the declaration of the method and throw appears in its body.