发生异常后如何继续运行代码?

时间:2018-10-22 07:39:00

标签: java xml spring exception-handling xml-parsing

我正在解析一个xml文件,其中有一些错误。我想通过Java解析文件并将所有解析异常打印在一个单独的文本文件中。但是,遇到第一个解析异常后代码停止运行。这样做的任何方式都可以使代码即使在解析过程中遇到异常后也可以运行。我希望所有错误都不是一个错误。

private final Document getDocument(String filepath)
        throws ParseException, TransformerException, SAXException, ParserConfigurationException, IOException {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);
    return doc;
}
public void printErrorsInXmlForParse() {
    String filepath = "//anypath";
    try {
        getDocument(filepath);


    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案