java - java : 'cannot find symbol' if not explicitly imported 中的自定义异常

标签 java exception compiler-errors

我试图在 java 中捕获异常,但是当捕获异常时,我得到 cannot find symbol错误。错误在 catch (ItemNotFoundException infe)并且编译器提示 ItemNotFoundException是没有已知的类(符号)。请注意 ItemNotFoundExceptionFeedController 类的成员.

引发错误的类:

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/

相关文章:

java - 有两种方法的 jUnit 失败

java - VisualVM 中的 "Total loaded"是什么意思?

c++ - 如何在我的项目包中使用 c++ 库 gtsam?

java - tomcat、jdk、jre如何背书?

asp.net - 编译错误: Server Error in '/' Application

lua - LuaRocks LuaSocket安装失败

java - String#split 根据 "\s+"拆分空字符串时的行为

java - 如何使用 Google Maps Android API 在 MapFragment 中绘制两点之间的路线

java - 关闭扫描器会抛出 java.util.NoSuchElementException

java - 当我尝试在finally block 中关闭BufferedReader时,为什么eclipse会提示?