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

  • SEARCH
  • Home
  • 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 6980563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:02:18+00:00 2026-05-27T18:02:18+00:00

I have a lot of boilerplate code that basically follows this pattern: function doSomething()

  • 0

I have a lot of boilerplate code that basically follows this pattern:

function doSomething() {
  try {
    [implementation]
    [implementation]
    [implementation]
    [implementation]
  } catch (Exception e) {
    MyEnv.getLogger().log(e);
  } finally {
    genericCleanUpMethod();
  }
}

I’d love to create my own annotation to clean my code up a bit:

@TryCatchWithLoggingAndCleanUp
function doSomething() {
  [implementation]
  [implementation]
  [implementation]
  [implementation]
}

The method signatures vary wildly (depending on the actual implementation of the method), but the boilerplate try/catch/finally part is always the same.

The annotation I have in mind would automatically wrap the contents of the annotated method with the whole try...catch...finally hoopla.

I’ve searched high and low for a straightforward way to do this, but have found nothing. I don’t know, maybe I just can’t see the woods for all the annotated trees.

Any pointers on how I might implement such an annotation would be greatly appreciated.

  • 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-27T18:02:19+00:00Added an answer on May 27, 2026 at 6:02 pm

    To do this, you would need some AOP framework that would use a proxy around your method. This proxy would catch the exception and execute the finally block. Quite frankly, if you don’t use a framework supporting AOP already, I’m not sure I would use one just to save these few lines od code.

    You could use the following pattern to do this in a more elegant way, though:

    public void doSomething() {
        logAndCleanup(new Callable<Void>() {
            public Void call() throws Exception {
                implementationOfDoSomething();
                return null;
            }
        });
    }
    
    private void logAndCleanup(Callable<Void> callable) {
        try {
            callable.call();
        } 
        catch (Exception e) {
            MyEnv.getLogger().log(e);
        } 
        finally {
            genericCleanUpMethod();
        }
    }
    

    I just used Callable<Void> as an interface, but you could define your own Command interface:

    public interface Command {
        public void execute() throws Exception;
    }
    

    and thus avoid the need to use a generic Callable<Void> and return null from the Callable.

    EDIT: in case you want to return something from your methods, then make the logAndCleanup() method generic. Here’s a complete example:

    public class ExceptionHandling {
        public String doSomething(final boolean throwException) {
            return logAndCleanup(new Callable<String>() {
                public String call() throws Exception {
                    if (throwException) {
                        throw new Exception("you asked for it");
                    }
                    return "hello";
                }
            });
        }
    
        public Integer doSomethingElse() {
            return logAndCleanup(new Callable<Integer>() {
                public Integer call() throws Exception {
                    return 42;
                }
            });
        }
    
        private <T> T logAndCleanup(Callable<T> callable) {
            try {
                return callable.call();
            }
            catch (Exception e) {
                System.out.println("An exception has been thrown: " + e);
                throw new RuntimeException(e); // or return null, or whatever you want
            }
            finally {
                System.out.println("doing some cleanup...");
            }
        }
    
        public static void main(String[] args) {
            ExceptionHandling eh = new ExceptionHandling();
    
            System.out.println(eh.doSomething(false));
            System.out.println(eh.doSomethingElse());
            System.out.println(eh.doSomething(true));
        }
    }
    

    EDIT : And with Java 8, the wrapped code can be a bit prettier :

    public String doSomething(final boolean throwException) {
        return logAndCleanup(() -> {                
            if (throwException) {
                throw new Exception("you asked for it");
            }
            return "hello";                
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a lot of class code which has some boilerplate like the following:
I have lot of code in our solution like this: Localization.Current.GetString(abc.def.gih.klm); I want to
We have lot of object with this kind of design : Interface and several
I have a lot of classes in the App_Code directory could this be a
I have a lot of constants that are somehow related, at some point I
I have a lot of code like the following, where I explicitly implement some
I have heard this a lot, the the Java API is poorly designed. Do
I notice that in a lot of template engines, in the HTML5 Boilerplate ,
I have lot of png sequences that I need to loop through and display
I have some Code that I'd like to share between an iOS and an

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.