gpt4 book ai didi

java - 如何抛出 StackOverflowException

转载 作者:行者123 更新时间:2023-11-29 04:56:29 28 4
gpt4 key购买 nike

我目前正在制作有界数组堆栈。此方法将大于存储容量的数组抛出到 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) in com.ld.BoundedArrayStack cannot implement push(T) in com.ld.StackADT overridden method does not throw com.ld.StackOverflowException

其他信息:

push(T) in com.Id.BoundedArrayStack clashes with push(T) in com.Id.StackADT; overridden method does not throw com.Id.StackOverflowException

我不明白这是什么问题。

最佳答案

根据您的错误消息,您的父类(super class) StackADT 定义了 push 不抛出 StackOverflowException,因此此方法的任何覆盖都不会抛出 StackOverflowException 要么。

您可以:

  • 将您的StackOverflowException 设为RuntimeException,它可以随时抛出。您不必在 throws 子句或
  • 中声明它
  • StackADT 中声明 push 方法,以便它throw StackOverflowException

关于java - 如何抛出 StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577941/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com