d-man
d-man

Reputation: 58113

Java - Spring AspectJ AfterThrowing

I have a method with following signature

I want to log exception after catching by my point cut, rite now when i execute the code it throws the exception print on the console then comes to my point cut defined method, may be because it is @AfterThoring annotation but there is no @BeforeThrowing annotation available ? please suggest what can i do

public void jingleBell(){
        System.out.println("Jingle Bell Job...");
        throw new RuntimeException("test error");
    }

and following advice with pointcut

@AfterThrowing(pointcut = "execution(* com.dc.lnwsk.adapter.Search.jingleBell())", throwing = "ex")
            public void handleException(Throwable ex){
                    //Log exception
            }

Upvotes: 0

Views: 978

Answers (1)

Yogendra Singh
Yogendra Singh

Reputation: 34397

Exceptions are not like methods where bye code can be altered(weaved) to call a new method before calling the point cut method.

Exceptions are run time phenomenon and JVM doesn't know in advance that some exception is going to be thrown and hence no @BeforeThrowing.

if you know your exception scenario up-front then better use @Before on the point cut method and handle the exception scenario.

Upvotes: 4

Related Questions