我目前正在制作有界数组堆栈。此方法将大于存储容量的数组抛出到 StackOverflowException
,除了我不断收到“throws StackOverflowException
部分
public void push(T item) throws StackOverflowException {
//push item onto the stack,
if (isFull()) throw new StackOverflowException();
else {
top++;
storage[top] = item;
}
}
错误信息:
Error:(52, 17) java:
push(T)
incom.ld.BoundedArrayStack
cannot implementpush(T)
incom.ld.StackADT
overridden method does not throwcom.ld.StackOverflowException
其他信息:
push(T)
incom.Id.BoundedArrayStack
clashes withpush(T)
incom.Id.StackADT
; overridden method does not throwcom.Id.StackOverflowException
我不明白这是什么问题。
最佳答案
根据您的错误消息,您的父类(super class) StackADT
定义了 push
不抛出 StackOverflowException
,因此此方法的任何覆盖都不会抛出 StackOverflowException
要么。
您可以:
- 将您的
StackOverflowException
设为RuntimeException
,它可以随时抛出。您不必在throws
子句或 中声明它
- 在
StackADT
中声明push
方法,以便它throw
StackOverflowException
。
关于java - 如何抛出 StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577941/