Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid using try...catch blocks
    primarykey
    data
    text
    <p>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.</p> <pre><code>public void iterateComparatorResults(ArrayList&lt;ComparatorValue&gt; results) throws IOException, IllegalArgumentException, IntrospectionException, IllegalAccessException, InvocationTargetException { Iterator&lt;ComparatorValue&gt; iterator = results.iterator(); for(int i=0; i&lt;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&lt;ArrayList&lt;ComparatorValue&gt;&gt; innerResults = results.get(i).getInnerBeans(); for(int r=0; r&lt;innerResults.size(); r++) { iterateComparatorResults(innerResults.get(r)); } } else { //Data does not match. runIteratorFalseAction(); } } } </code></pre> <p>Everytime I reference this particular method is any other class, I have to use a try catch block that looks like this. </p> <pre><code>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(); } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload