How to deal with method which throws 7 exceptions I'm re factoring some code and have come across a method which is throwing 7 exceptions, the method length is approx 20 lines. The method caller wraps this method in a try catch, and just catches the generic Exception. Is there a better way to handle this ? I don't think I should handle each possible exception thrown individually ?

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

    private void setter() throws MalformedURLException, IOException, ParserConfigurationException, SAXException, TransformerFactoryConfigurationError, TransformerException, XPathExpressionException
    {       
        .....   
    }
This depends on what you want to do in case of an exception. If your handling is just printing the stack trace your solution is OK. But you might want to react in a different way depending on the problem.

Just an example:


an IO problem could mean that you have to retry (in case on network)
a malformed URL might mean that you have to generate an error message and ask the user to re-enter the data
and so on


If you do not try to recover from the error, I don't know if it makes sense to catch the exception, print the stack trace and then continue. It might be the same as letting the application crash (and print the exception anyway).