gpt4 book ai didi

Java - Spring AspectJ AfterThrowing

转载 作者:行者123 更新时间:2023-12-01 18:56:36 25 4
gpt4 key购买 nike

我有一个带有以下签名的方法

我想在捕获切点后记录异常,现在当我执行代码时,它会在控制台上抛出异常打印,然后到达我的切点定义的方法,可能是因为它是 @AfterThoring 注释,但有没有可用的 @BeforeThrowing 注释?请建议我能做什么

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

并遵循切入点的建议

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

最佳答案

异常与方法不同,在方法中,可以在调用切入点方法之前更改(编织)代码以调用新方法。

异常是运行时现象,JVM 事先并不知道会抛出某些异常,因此没有@BeforeThrowing

如果您预先知道异常情况,那么最好在切入点方法上使用@Before并处理异常情况。

关于Java - Spring AspectJ AfterThrowing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13753082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com