Search code examples
javaexceptionxml-parsingsax

How to determine that no exceptions have been caught java


I have a code for my xml file validation:

try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setValidating(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

            public void error(SAXParseException exception) throws SAXException {
                System.out.println("Error: " + exception.getMessage());
            }
            public void fatalError(SAXParseException exception) throws SAXException {
                System.out.println("Fatal error: " + exception.getMessage());
            }

            public void warning(SAXParseException exception) throws SAXException {
                System.out.println("Warning: " + exception.getMessage());
            }
        });

            Document doc = builder.parse(xml);
        } catch (SAXException e) {
            e.printStackTrace();

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

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

        } 

I can't figure out how should I organize my exception handling, so I could print message "File is valid!" like this System.out.println(xml + " is valid!"); or similarly.


Solution

  • I managed to solve my problem. I simply had to add throw exception; lines to my ErrorHandler methods so I could track a validation process.