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 7444683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:34:06+00:00 2026-05-29T11:34:06+00:00

In some old Java code, I found a class that contains a lot of

  • 0

In some old Java code, I found a class that contains a lot of methods that all use the same error handling code (try-catch with a lot of error handling, logging and so on). It looks like the first method was simply copied and then the code in the try block was slightly adapted. Here is what it basically looks like:

public class myClass{

    public void doSomething() {
        try {
           //do something
        } catch (Exception e) {
          //extensive error handling
        }
    }

    public void doSomethingElse() {
        try {
           //do something else
        } catch (Exception e) {
          //extensive error handling, copy-pasted from the above method
        }
    }

}

How could this be simplified? I don’t want to change the interface of the class (much), I’d just like to get rid of the copy-pasted catch blocks, so that only the code from the try block stays within the original method.

I thought about using the Factory Method pattern, where one method implements the error handling and calls the original method in the try block. But then, all calls would have to go through this method.

Any ideas?

  • 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-29T11:34:07+00:00Added an answer on May 29, 2026 at 11:34 am

    Simplify it the same way you simplify all other repeated code: Put the repeated code in a method, and call the method:

    public void doSomething() {
        try {
            //do something
        } catch (Exception e) {
            handleError(e);
        }
    }
    
    public void doSomethingElse() {
        try {
            //do something else
        } catch (Exception e) {
            handleError(e);
        }
    }
    
    private void handleError(Exception e) {
        //extensive error handling
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote some data access methods (plain old Java) that use immutable objects
I'm profiling some old java code and it appears that my caching of values
I am trying to make some small additions to some old java code that
I have some old code in Javascript that interfaces to Java DWR . I'm
I am in process of reviewing some old Java/JDBC code for CLOB-handling on oracle
I have some old code (pre Java 1.5) that uses classes to implement type
I have some existing code that accepts a java.sql.ResultSet that contains info retrieved from
I'm converting some old Java code to Scala. I have this. class XYCustomRenderer(dataMax: Double,
I'm trying to clean up some warnings in some old Java code (in Eclipse),
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)

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.