简体   繁体   中英

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
            }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM