How can the exception be dealt with correctly in this case?



  • I have Aspect. And there's a tip.loggingService()) which I will apply to my target method, which I will transfer control through proceedingJoinPoint.proceed();

    And in doing so, I've got a target method that can throw out an exception, for example, some sort of thing. SomeExeption♪ How am I supposed to handle it, turn it around? First option or second? Or something else.

    (1)

    public Object loggingService(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LOGGER.info("Before invoking  method" + " " + "\"" + proceedingJoinPoint.getSignature().getName().toString() + "\"");
        Object value = null;
        try {
            value = proceedingJoinPoint.proceed(); 
        } catch (Throwable e) {
            LOGGER.error("Error during execution method");
            throw new Throwable(e);
        }
        LOGGER.info("After invoking  method");
        return value;
    }
    

    (2)

    public Object loggingService(ProceedingJoinPoint proceedingJoinPoint) throws SomeException {
        LOGGER.info("Before invoking  method" + " " + "\"" + proceedingJoinPoint.getSignature().getName().toString() + "\"");
        Object value = null;
        try {
            value = proceedingJoinPoint.proceed(); 
        } catch (Throwable e) {
            LOGGER.error("Error during execution method");
            throw new SomeException(e);
        }
        LOGGER.info("After invoking  method");
        return value;
    }
    


  • Since your service is exclusively logical and has no information on what to do with exemptions, all exceptions need to be discarded as such, without any change or orientation. Otherwise, you can get unprocessed exceptions, or light your loggingService in the stacktraze, which is absolutely nothing.

    try {
        value = proceedingJoinPoint.proceed(); 
    } catch (Throwable e) {
        LOGGER.error("Error during execution method");
        throw e; // пробрасываем оригинальное исключение дальше без изменения стектрейса
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2