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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:24:48+00:00 2026-06-07T00:24:48+00:00

Does anyone know if there is a way to generate different code in the

  • 0

Does anyone know if there is a way to generate different code in the catch block automatically depending on the exception?

The Eclipse function ‘Surround with try/catch’ generates a try/catch block which just includes dumping a stack trace.

I’m doing a bunch of similar things in the code and so most of my exceptions will boil down to probably three or so different types. I’d like to have different catch block code for each one and have eclipse auto format based on the exception.

For example:
if my code generates a RemoteConnectionException I’d like to display a dialog to the user to reconnect.
If it generates a RemoteContentException I’d like to log it.

(I made these up.)

Thanks in advance

UPDATE:
I’ve been poking around and have two potential solutions.

1) I’ve found something called the fast code plugin which might do what I’m looking for.
http://fast-code.sourceforge.net/index.htm

2) For specifically handling exceptions I’ll probably just write a generic exception handler and modify the catch block code to pass the exception to that instead of printing the stack trace. Then the java code will determine which action to take based on exception type.

  • 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-06-07T00:24:50+00:00Added an answer on June 7, 2026 at 12:24 am

    Templating has it’s limits. However your problem can be solved very elegantly with Aspect. ( http://www.eclipse.org/aspectj/ ) Just create a new annotation for every type of “template-case” you need and use an around advice.

    Ps: don’t use printStackTrace() to syserr/sysout. There are so many production grade, lightweight logging frameworks…. pleeeaseee… don’t abuse poor little System.out/err 🙂

    EDIT:

    Some example for a logging / benchmarking advice. (note: I’m using spring AOP for aspects, and lombok for easy access to the logging framework. The getCurrentUser() code is not really relevant here, it’s just for getting the current user from Spring Security)

    package com.XXXXXXXX.aspects;
    
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    @Slf4j
    public class LoggerAspect {
    
        private final static String DOMAIN = "XXXXXXXX";
    
        private static String getCurrentUser() {
            String username = "Unknown";
            try {
                Object principal = SecurityContextHolder.getContext().
                        getAuthentication().
                        getPrincipal();
                if (principal instanceof UserDetails) {
                    username = ((UserDetails) principal).getUsername();
                } else {
                    username = principal.toString();
                }
            } catch (Exception e) {
            }
            return username;
        }
    
        @Pointcut("within(com.XXXXXXXX.services..*)")
        public void inServiceLayer() {
        }
    
        @Pointcut("execution(* getMatcherInfo(..)) || execution(* resetCounter(..))")
        public void notToAdvise() {
        }
    
        @Around("com.XXXXXXXX.aspects.LoggerAspect.inServiceLayer() && !com.XXXXXXXX.aspects.LoggerAspect.notToAdvise()")
        public Object doLogging(ProceedingJoinPoint pjp)
                throws Throwable {
            long start = System.nanoTime();
            StringBuilder sb = new StringBuilder(DOMAIN);
            sb.append('/').
                    append(getCurrentUser()).
                    append(" accessing ").
                    append(pjp.getSignature().
                    getDeclaringTypeName()).
                    append('.').
                    append(pjp.getSignature().
                    getName());
            log.trace("START: " + sb.toString());
            Object retVal = pjp.proceed(pjp.getArgs());
            long duration = System.nanoTime() - start;
            log.trace("STOP: " + duration / 1000000 + " msec. " + sb.toString());
            return retVal;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know is there a way to implement Windows Live ID authentication into
Does anyone know there have any other way that (by not using json_encode and
Does anyone know if there's a way to check the number of messages in
Does anyone know if there's a way to access past app installs via the
Does anyone know do there have any way that I can encrypt the array
Does anyone know if there's a way to just select the number of rows
Does anyone know if there's a way to format the date generated by strftime
Does anyone know if there is a way to get a list of all
Does anyone know how to read a x.properties file in Maven. I know there
Does anyone know if there are new binaries for the castle logging facility using

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.