user3756506
user3756506

Reputation: 451

Spring exception interceptor

I have such code:

 public void method1(){
  try{
      method2();
    }catch(Exception e){
        e.printStackTrace();
    }
 }
 public void method2(){
       throw new RuntimeException();
    }

I need to create an annotation, that will handle this exception and throw another one.

@MyExceptionAnnotation(message="MyExceptionTest", value = MyException.class)
public void method2(){
       throw new SpringException();
    }

I tried to make beanpostprocessor for it, but it didn't work work.

Upvotes: 1

Views: 321

Answers (1)

Artem Ptushkin
Artem Ptushkin

Reputation: 1299

You should not give to method info how to handle exception thrown by one. It is the responsibility of a method consumer. If you what do something eq throw exception then

catch(Exception e) { exceptionHandler.handle(e); } Where exceptionHandler knows how to handle every exception. Or just see @ExceptionHandler

If you still want to have a specific way to handle specific method exception see AOP and spring-aspect

Upvotes: 1

Related Questions