【问题标题】:How to catch the exception thrown from a Spring AOP Advice如何捕获从 Spring AOP Advice 抛出的异常
【发布时间】:2010-08-03 01:33:21
【问题描述】:

我有自己的异常“MyOwnException”并从我的服务类中抛出此异常

public void service() throws MyOwnException
{
// some code 

}

现在我想在建议中捕获 MyOwnException 并重新抛出一个全新的异常

public class SimpleThrowsAdvice implements ThrowsAdvice {

    public void afterThrowing(Method method, Object[] args, Object target,
                MyOwnException ex) throws Throwable {
        throw new Exception("new Description",ex);
    }
}

现在,我怎样才能从上面的建议SimpleThrowsAdvice 中捕捉到重新抛出的Exception

【问题讨论】:

  • 您确定要重新捕获已经捕获但现在已包装的异常吗?这听起来太像海龟了。您打算用 SimpleThrowsAdvice 无法执行的新异常捕获建议做什么?

标签: java spring aop spring-aop


【解决方案1】:

您应该使用 Around 建议来做到这一点。看这里Spring AOP AfterThrowing vs. Around Advice

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2016-09-16
    • 2011-05-22
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多