我在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行)中给出了一条错误消息,指出“未处理的异常类型异常”,即使在实践中,此异常在此代码中永远不会发生。我可以在Eclipse中禁用这个错误,这样它就不会困扰我了吗,或者有没有其他方法可以处理这个错误?
提前感谢你的回答!
发布于 2018-10-20 06:59:38
使用try/catch将构造函数包围起来以捕获异常。
如下所示:
public class Foo {
try {
public static final Bar = new Bar(20);
}catch(InvalidCharacterException e) {
e.PrintStackTrace();
}
应该可以解决你的问题。如果没有,请随时回复,我将尝试进一步帮助您。
https://stackoverflow.com/questions/52900734
复制相似问题