如何捕获 Spring AOP Advice 抛出的异常

发布于 2024-09-12 17:56:09 字数 515 浏览 2 评论 0原文

我有自己的异常“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

I have my own exception "MyOwnException" and throw this exception from my service class

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

}

Now I want to catch MyOwnException in an advice and rethrow a brand-new Exception

public class SimpleThrowsAdvice implements ThrowsAdvice {

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

Now, how can I catch the re-thrown Exception from the above Advice SimpleThrowsAdvice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-09-19 17:56:09

您应该使用周围建议来做到这一点。请参阅此处 Spring AOP AfterThrowing 与 around Advice

You should use the Around advice to do that. See here Spring AOP AfterThrowing vs. Around Advice

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文