Re-throw an exception in @Around Aspect
本问题已经有最佳答案,请猛点这里访问。
在执行某些操作后,是否会在REST控制器中从around方面向
1 2 3 4 5 6 7 8 9 10 11 12 | @Around("execution(* *(..)) && @annotation(someAnnotation)") public Object catchMethod(ProceedingJoinPoint point, SomeAnnotation someAnnotation) throws Throwable { //some action try { result = point.proceed(); } catch (Exception e) { //some action throw e; //can I do this? } //some action return result; } |
这是可行的,但我不知道也许我出于某种原因没有这样做。
一个通知(不是为实现某种异常魔力而设计的)不应该吞咽由advised方法抛出的异常。
所以是的,如果你在
如果您不需要在方法执行(成功)之后在通知中进行一些处理,那么可以省略完整的异常处理。
1 2 3 4 5 |
如果您需要在通知调用之后进行一些处理,那么使用try-catch finally bock。catch子句是可选的,但必须重新引发异常
1 2 3 4 5 6 7 8 9 10 11 12 | public Object catchMethod(ProceedingJoinPoint point, SomeAnnotation someAnnotation) throws Throwable { //some action try { Result result = point.proceed(); //some action that are executed only if there is no exception } catch (Exception e) { //some action that are executed only if there is an exception throw e; //!! } finally { //some action that is always executed } } |