gpt4 book ai didi

java - 如何使 Eclipse 在未捕获声明的异常时报告错误?

转载 作者:行者123 更新时间:2023-12-02 06:20:58 24 4
gpt4 key购买 nike

当声明的异常没有被捕获时,如何让Eclipse报告错误?
例如,如果我将方法声明为

public int someMethod(int a, int b) throws IllegalArgumentException {
...
}

然后在另一种方法中使用它,例如

public int anotherMethod() {
...
return someMethod(a, b);
}

我希望编译器在 anotherMethod 中报告错误,直到我捕获 IllegalArgumentException 或将另一个方法声明为

public int anotherMethod() throws IllegalArgumentException {}

最佳答案

Eclipse 实际上应该报告错误,因为 anotherMethod() 中有一个未捕获的 IOException:

Unhandled exception type IOException

只有未经检查的异常(例如 RuntimeException)才会导致您所描述的行为,但 IOException 是受检查的异常。

也许你已经关闭了 eclipse 中的错误报告,但我对此表示怀疑。您可以在首选项/常规/编辑器/文本编辑器/注释/错误中检查这一点。确保选中所有复选框。

注意:我的答案是指编辑之前提出的原始问题,其中提到了 IOException 而不是 IllegalArgumentException。

关于java - 如何使 Eclipse 在未捕获声明的异常时报告错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21038779/

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