在Eclipse Java中禁用错误提示/警告

在Eclipse Java中禁用错误提示/警告,java,eclipse,ide,Java,Eclipse,Ide,我在Eclipse中使用以下代码: public class Foo { public static final Bar bar = new Bar(20); } public class Bar { public int value; // This needs to be able to be called in places other than // just Foo, and there it will need to throw. publ

我在Eclipse中使用以下代码:

public class Foo {
    public static final Bar bar = new Bar(20);
}

public class Bar {
    public int value;

    // This needs to be able to be called in places other than
    // just Foo, and there it will need to throw.
    public Bar(int value) throws Exception {
        if(value == 0) {
            throw Exception("Error. Value cannot be 0 when constructing Bar.");
        }
        return;
     }
}
这在Foo(第2行)中给了我一条错误消息,上面写着“Unhandled exception type exception”,即使在实践中,此代码永远不会出现此异常。我可以在Eclipse中禁用这个错误,这样它就不会困扰我,或者有其他方法可以处理这个错误吗


提前感谢您的回答

用try/catch包围构造函数以捕获异常

像这样:

public class Foo {

try {
    public static final Bar = new Bar(20);
}catch(InvalidCharacterException e) {
    e.PrintStackTrace();
}

应该解决你的问题。如果没有,请随时回复,我将尝试进一步帮助您。

这是一个需要修复的编译器错误,需要编译Java代码,并且不是Eclipse问题:检查的异常需要在Java中显式处理,方法是使用
try
-
catch
或传递异常(方法/构造函数
抛出
…)

如果无法更改类
Bar
,一种可能是使用私有静态方法初始化常量
Bar
(根据Java命名约定,该常量应命名为
Bar
):


这仍然会产生错误。不能在类主体中但在方法块之外添加try-catch语句。另外,欢迎使用堆栈溢出!我认为你需要一个类构造函数,如果你没有,你应该把Foo类转换成main类。将try/catch代码放在主类constructor中。Foo.bar是一个静态变量,因此应该能够在没有实际Foo实例的情况下访问它,因此构造函数不需要存在就可以初始化bar。您到底想用它做什么?我认为Eclipse不太乐意在创建类时出现潜在的异常。您总是可以从Bar类中删除异常抛出,它可能会工作。但是如果你真的想抛出异常,你需要将Bar对象创建代码从Foo类的顶部移走?我只想知道如何解决这个问题。显示的源代码中没有引用InvalidCharacterException,而且它也不是标准Java运行时的一部分,所以在哪里引用它?知道不会引发异常不会更改被声明为可能引发异常的方法的语义。如果不想捕获异常,请使用诸如
IllegalArgumentException
public class Foo {

    public static final Bar BAR = initBar(20);

    private static Bar initBar(int value) {
        try {
            return new Bar(20);
        } catch (InvalidCharacterException e) {
            return null;
        }
    }

}