Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:08:28+00:00 2026-05-29T06:08:28+00:00

I understand these are necessary…of course to write proper code, but is there a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:08:29+00:00Added an answer on May 29, 2026 at 6:08 am

    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:

    1. Handle the exception at the point at which it’s thrown, if you can (to close an open file on a file access exception, for instance, or retry a troublesome interface (stupid unreliable bespoke USB devices…)).
    2. Allow the exception to bubble up to a point higher in your stack, where you can catch all exceptions you can’t otherwise handle. Log them, display them, or kill your application, whatever makes the most sense.

    I’ll illustrate the second point with some code, stolen from inspired by the Java tutorials.

    private String readFirstLineFromFile(String path) throws IOException
    {
        try (BufferedReader br = new BufferedReader(new FileReader(path)))
        {
            return br.readLine();
        }
    }
    

    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 throws clause. That calling method may handle the exception itself, or pass the buck to its caller, with the same clause:

    private String readAllTheFiles() throws IOException
    {
        for (...)
        {
            readFirstLineFromFile(...);
        }
    }
    

    Now, there’s lots of debate around why Java requires the throws in this case. Many other languages don’t use them, for better or worse. You’ll often see RuntimeExceptions – exceptions that don’t require a throws clause. If your method might throw an exception that extends from RuntimeException, you don’t need to declare that fact in a throws.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I grabbed this code form JCarousel and just trying to understand these lines below.
I understand that these methods are for pickling/unpickling and have no relation to the
I understand it's a standard practice to look at both these variables. Of course
Can you help me understand the practical differences between these two; IList<int> myList =
I've been researching the difference between these two patterns. I understand that facade encapsulates
I understand that there are several ways to blend XNA and WPF within the
I understand that there's no universal answer to the attribute vs. element debate (and
I'm kind of writing a little game engine -- purely to understand how these
I understand there is a HTTP response header directive to disable page caching: Cache-Control:no-cache
Something I'm not to clear about, I understand there are differences between C# and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.