Alr*_*ail 3 java aop spring spring-aop
在执行某些操作后,将异常从周围方面重新抛出到ExceptionHandlerrest控制器中是否正常,如下所示:
@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;
}
Run Code Online (Sandbox Code Playgroud)
它正在工作,但我不知道也许出于某种原因我没有这样做。
一个建议(并非旨在做一些异常魔术)不应吞噬由所建议的方法引发的异常。
所以是的,如果您有尝试陷阱,point.proceed()那么应该重新抛出异常。
如果在方法成功执行后不需要通知中的某些处理,则可以省略完整的异常处理。
@Around("execution(* *(..)) && @annotation(someAnnotation)")
public Object catchMethod(ProceedingJoinPoint point, SomeAnnotation someAnnotation) throws Throwable {
//some action
return point.proceed();
}
Run Code Online (Sandbox Code Playgroud)
如果您需要在建议调用之后进行一些处理,请使用try-catch-finally组件。catch子句是可选的,但是您必须重新抛出异常
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3233 次 |
| 最近记录: |