I understand these are necessary…of course to write proper code, but is there a design pattern that will help avoid having to repeat try…catch blocks repeatedly in a class? For instance, I wrote a particular class that throws 5 different exceptions.
public void iterateComparatorResults(ArrayList<ComparatorValue> results) throws IOException, IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException {
Iterator<ComparatorValue> iterator = results.iterator();
for(int i=0; i<results.size(); i++) {
//Set data variables.
setBeans(results.get(i).getClientBean(), results.get(i).getServerBean());
setValues(results.get(i).getClientValue(), results.get(i).getServerValue());
if(results.get(i).isMatch()) {
//Data matches.
runIteratorTrueAction();
} else if(results.get(i).getInnerBeans() != null){
//Value is a nested bean. Iterate again.
ArrayList<ArrayList<ComparatorValue>> innerResults = results.get(i).getInnerBeans();
for(int r=0; r<innerResults.size(); r++) {
iterateComparatorResults(innerResults.get(r));
}
} else {
//Data does not match.
runIteratorFalseAction();
}
}
}
Everytime I reference this particular method is any other class, I have to use a try catch block that looks like this.
try {
beanComparator.setIteratorFalseAction(falseAction);
beanComparator.iterateComparatorResults(beanComparator.compareBeans(contact, compareContact));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
I am hoping there is some design pattern I can implement so I can create a separate class or something and hide all the try blocks in one place then reference that class and use the try blocks without actually having to write them. Similar to the way that other frameworks like Spring do? I just need a little direction on how to do this as I have no idea even where to begin.
I’m not sure how real-world your example is, but there’s no need to a) catch each exception separately, if you don’t handle them differently, or b) catch them at all, in many cases.
What you’re doing in your example, just printing a stack trace, is a bad idea in almost all cases. You’re printing messy information to a place where no one’s likely paying attention.
I follow two general guidelines:
I’ll illustrate the second point with some code,
stolen frominspired by the Java tutorials.Here, we’ve defined a method that attempts to read from a file. Lots of things can go wrong here, but there’s nothing the code at this level is going to do about that. Instead, it’s going to pass the buck to the method that called it, with the
throwsclause. That calling method may handle the exception itself, or pass the buck to its caller, with the same clause:Now, there’s lots of debate around why Java requires the
throwsin this case. Many other languages don’t use them, for better or worse. You’ll often seeRuntimeExceptions– exceptions that don’t require athrowsclause. If your method might throw an exception that extends fromRuntimeException, you don’t need to declare that fact in athrows.