2

I have the following class, which encapsulates some test data. I need only one instance of it, so I create an enum.

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);
    }
}

At the declaration of its only element, INSTANCE; I get the following compiler error in IntelliJ Idea:

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

How can I fix it, given that it occurs at the line, where the element is defined, not a method?

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • Possible duplicate of [How to throw an exception from an enum constructor?](http://stackoverflow.com/questions/3543903/how-to-throw-an-exception-from-an-enum-constructor) – wero Oct 10 '15 at 11:51

1 Answers1

3

How can I fix it, given that it occurs at the line, where the element is defined, not a method?

Under the hood Java creates an instance INSTANCE by calling the constructor. The code on the declaration line looks similar to this:

public static final INSTANCE = new ErnstReuterPlatzBuildings();

That's why the error is coming from that line.

As far as I know, there is no way to fix this by allowing the user to catch a checked exception, because INSTANCE is initialized in a context outside of any method call.

You can work around this issue by catching the exception yourself, and wrapping it in an unchecked RuntimeException of an appropriate type or even an 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);
    } 
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523