我试图在 java 中捕获异常,但是当捕获异常时,我得到 cannot find symbol
错误。错误在 catch (ItemNotFoundException infe)
并且编译器提示 ItemNotFoundException
是没有已知的类(符号)。请注意 ItemNotFoundException
是 FeedController
类的成员.
引发错误的类:
public class WebResource {
public Entry getEntry() {
String id = "490d6c15";
try {
Entry entry = FeedController.getInstance().getEntry(id);
} catch (ItemNotFoundException infe) {
return null;
}
return FeedController.getInstance().getEntry("490d6c15");
}
}
引发异常的 FeedController 类:
public class FeedController {
public Entry getEntry(String id) throws ItemNotFoundException {
if(!index.containsKey(id)) throw new ItemNotFoundException(id);
return index.get(id);
}
public static class ItemNotFoundException extends Exception {
public ItemNotFoundException(String id) {
super(String.format("item with id %s does not exist", id));
}
}
}
我还尝试无情地导入 WebResource 中 FeedController 导入的所有内容,以消除丢失的导入。
最佳答案
无情地导入由引发此异常的类导入的所有内容可能不会为您提供包含异常的包。找到异常的全称;它应该在您正在调用的方法的 javadoc 中。
关于java - java : 'cannot find symbol' if not explicitly imported 中的自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24353695/