I am trying to implement a stack with some functions in Java. I have created the class UnderflowException that implements Exception like this:
package exceptions;
public class UnderflowException extends Exception
{
public UnderflowException(String err)
{
super(err);
}
}
When I'm implementing the Interface I get the following error "No exception of type UnderflowException can be thrown; an exception type must be a class of Throwable" when I try to throw.
My interface looks like this:
import exceptions.*;
public interface Stack
{
public void push(Object x);
public void pop() throws UnderflowException;
public Object top() throws UnderflowException;
//other functions
}
Is there a problem with the UnderflowException class? Thank you!
Replace Exception
with java.lang.Exception
. Looks like you use wrong class and FQN help solve problem.