【问题标题】:Why am I getting this compiler error (using enum as singleton)?为什么我会收到此编译器错误(使用枚举作为单例)?
【发布时间】:2015-10-10 11:32:13
【问题描述】:

我有以下类,它封装了一些测试数据。我只需要它的一个实例,所以我创建了一个枚举。

public enum ErnstReuterPlatzBuildings {
    INSTANCE; // Compiler error occurs here
    private final Map<String, IBuilding> buildingsByIds;
    ErnstReuterPlatzBuildings() throws ParserConfigurationException,
        SAXException, XPathExpressionException, IOException {
        this.buildingsByIds = composeBuildingsByIds(
            getBuildings(ErnstReuterPlatzBuildings.class)
        );
    }
    public Map<String, IBuilding> getBuildingsByIds() {
        return buildingsByIds;
    }
    public static Document readErnstReuterPlatzData(final Class clazz)
        throws ParserConfigurationException, SAXException, IOException {
        final InputStream stream =
            clazz.getClassLoader()
                .getResourceAsStream("mc/ernstReuterPlatz/map.osm");
        final DocumentBuilderFactory dbfac =
            DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        return docBuilder.parse(stream);
    }

    private Map<String, IBuilding> composeBuildingsByIds(
        final Set<IBuilding> buildings) {
        final Map<String,IBuilding> buildingsByIds = new HashMap<>();
        for (final IBuilding building : buildings) {
            buildingsByIds.put(building.getWayId(), building);
        }
        return buildingsByIds;
    }

    private Set<IBuilding> getBuildings(final Class clazz)
        throws ParserConfigurationException, SAXException, IOException,
        XPathExpressionException {
        final Document doc = readErnstReuterPlatzData(clazz);
        final PointsReader pointsReader = new PointsReader(doc);
        pointsReader.init();
        final BuildingExtractor testObject =
            new BuildingExtractor(pointsReader);
        return testObject.extractBuildings(doc);
    }
}

在声明其唯一元素 INSTANCE; 时,我在 IntelliJ Idea 中收到以下编译器错误:

Error:(22, 5) java: unreported exception javax.xml.parsers.ParserConfigurationException; must be caught or declared to be thrown

如果它发生在 element 定义的行,而不是方法,我该如何解决?

【问题讨论】:

标签: java enums singleton


【解决方案1】:

如果它发生在定义元素的行,而不是方法,我该如何解决?

在底层,Java 通过调用构造函数创建了一个实例INSTANCE。声明行的代码类似于:

public static final INSTANCE = new ErnstReuterPlatzBuildings();

这就是错误来自该行的原因。

据我所知,没有办法通过允许用户捕获已检查异常来解决此问题,因为 INSTANCE 是在任何方法调用之外的上下文中初始化的。

您可以通过自己捕获异常并将其包装在未经检查的适当类型的RuntimeException 甚至ExceptionInInitializerError 中来解决此问题:

ErnstReuterPlatzBuildings() {
    try {
        this.buildingsByIds = composeBuildingsByIds(
            getBuildings(ErnstReuterPlatzBuildings.class)
        );
    } catch (ParserConfigurationException pce) {
        throw new ExceptionInInitializerError(pce);
    } catch (SAXException sxe) {
        throw new ExceptionInInitializerError(sxe);
    } catch (XPathExpressionException xpe) {
        throw new ExceptionInInitializerError(xpe);
    } catch (IOException ioe) {
        throw new ExceptionInInitializerError(ioe);
    } 
}

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多