简体   繁体   中英

Is catching DocumentException a good idea?

I have a utility method which reads an xml file and converts to string as below:

public static String readFile(String xmlFileName) throws IOException, DocumentException{
        String xmlMsg = null;
        Resource resource = null;
        InputStream inputStream = null;
        try{
            resource = new ClassPathResource(xmlFileName);
            inputStream = resource.getInputStream();
            SAXReader reader = new SAXReader();
            Document doc = reader.read( inputStream );
            xmlMsg = doc.asXML();
        }finally{
            if(inputStream != null){
                inputStream.close();
            }
        }
        return xmlMsg;
    }

Is it a bad idea if I catch the DocumentException in the above code and rethrow it as below:

public static String readFile(String xmlFileName) throws IOException, DocumentException{
        String xmlMsg = null;
        Resource resource = null;
        InputStream inputStream = null;
        try{
            resource = new ClassPathResource(xmlFileName);
            inputStream = resource.getInputStream();
            SAXReader reader = new SAXReader();
            Document doc = reader.read( inputStream );
            xmlMsg = doc.asXML();
        }catch (DocumentException e){
           throw new DocumentException("some message");
        }finally{
            if(inputStream != null){
                inputStream.close();
            }
        }
        return xmlMsg;
    }

So, is it a bad idea to leave the responsibility of handling the DocumentException to the caller?

No, leaving the caller to handle the Exception is fine - throw early catch late .

What I have a problem with is this:

}catch (DocumentException e){
    throw new DocumentException("some message");

Why would you catch (DocumentException e) and then throw a new instance that strips out all useful information ? You can simply not catch it in the first place and let it percolate up to someone who can handle it.

Also, use Java 7 try-with-resources rather than finally . So, you code should be:

public static String readFile(String xmlFileName) throws IOException, DocumentException {
    try (final InputStream is = new ClassPathResource(xmlFileName).getInputStream()) {
        final SAXReader reader = new SAXReader();
        final Document doc = reader.read(inputStream);
        return doc.asXML();
    }
}

I have removed the variables that are declared as null then reassigned, I hate this practice and so do many other Java developers - get out of this habit. Declare things when you need them and assign them immediately . In a garbage collected language the principal of minimum scope is very important.

I have also changed it to return directly rather than storing the value for some reason.

    catch (DocumentException e){ // here you are catching exception
       throw new DocumentException("some message");// now you are changing
       // exception message 
    }

That is a bad practice you have to throw originally thrown exception to upper level then you can handle the Exception there.

This way is better

   catch (DocumentException e){
       throw new DocumentException("some message",e);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM