Customize Try / Catch Pattern Based on Exception

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

The Eclipse "Surround with try / catch" function generates a try / catch block that only includes resetting the stack trace.

I do a bunch of similar things in code, and so most of my exceptions will thicken to three or more different types. I would like to have a different catch lock code for each of them and have an automatic exception-based eclipse format.

For example: if my code throws a RemoteConnectionException, I would like to display a dialog box for reconnecting the user. If it throws a RemoteContentException, I would like to register it.

(I did it.)

Thanks in advance

UPDATE: I have already said and have two potential solutions.

1) I found something called a quick code plugin that can do what I'm looking for. http://fast-code.sourceforge.net/index.htm

2) In order to specifically handle exceptions, I will probably just write a general exception handler and modify the catch block code to pass an exception instead, and not print a stack trace. Then the java code will determine what action to take based on the type of exception.

+5
source share
2 answers

. Aspect. (http://www.eclipse.org/aspectj/) "-", , .

Ps: printStackTrace() syserr/sysout. , .... pleeeaseee... System.out/err:)

EDIT:

/. (: spring AOP , lombok - . getCurrentUser() , 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;
    }
}
+4

, eclipse. try/catch e.printStackTrace() catch Exception e.

0

All Articles