在EJB3中处理拦截器中的异常的正确方法是什么? [英] What is the proper way to deal with Exceptions in Interceptors in EJB3?

查看:116
本文介绍了在EJB3中处理拦截器中的异常的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的bean使用拦截器,它将检查给定连接令牌的有效性。

I want to use an interceptor for my bean, which will check the validity of a given connection token.

如果连接无效,我想抛出一个特定的异常,如果连接过期,我想发送另一个(TokenExpiredException,类似这样的东西)。这些异常包含在给客户端的接口中。

If the connection is invalid, I want to throw a particular exception, if the connection expired, I want to send another (TokenExpiredException, something like this). These Exceptions are included in the interface given to the client.

@AroundInvoke
public Object checkParams(InvocationContext ctx) throws TokenExpiredException, Exception{
    //code to check token
    //...
    throw new TokenExpiredException(); 
}

根据我的尝试,在拦截器中抛出这种特定的异常会导致 UndeclaredThrowableException 在客户端。尽管此异常包括对原因的引用,但它并不是真正理想的方法,不能用常规的catch子句处理。

From what I tried, throwing such specific Exception in the Interceptor leads to an UndeclaredThrowableException on the client side. While this exception includes the reference to the cause, it is not really ideal, and can't be dealt with with regular catch clauses.

那么正确的方法是声明与拦截器不同的异常类型?

What is the correct way then to declare different Exception types with Interceptors?

推荐答案

我认为没有正确的方法。方法应该只抛出它们声明的异常,并且拦截器不应添加新的异常。

I don't think there is a correct way to do that. Methods should throw only the exceptions they declared, and an interceptor shouldn't add a new one.

我的个人情况是通过在默认异常中添加错误代码来解决的被所有方法抛出。

My personal case got fixed by adding an error code to our default exception which is thrown by all methods.

这篇关于在EJB3中处理拦截器中的异常的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆