获取UndeclaredThrowableException而不是我自己的异常 [英] getting UndeclaredThrowableException instead of my own exception
问题描述
我有以下代码
public Object handlePermission(ProceedingJoinPoint joinPoint, RequirePermission permission) throws AccessException, Throwable {
System.out.println("Permission = " + permission.value());
if (user.hasPermission(permission.value())) {
System.out.println("Permission granted ");
return joinPoint.proceed();
} else {
System.out.println("No Permission");
throw new AccessException("Current user does not have required permission");
}
}
当我使用的用户没有权限,我得到 java.lang.reflect.UndeclaredThrowableException
而不是 AccessException
。
When I use a user that does not have permissions, I get java.lang.reflect.UndeclaredThrowableException
instead of AccessException
.
推荐答案
AccessException
是一个已检查的异常,但它是从没有在其 throws
子句中声明它的方法(实际上 - 从拦截该方法的方面)。这是Java中的一个异常情况,所以你的异常包含在 UndeclaredThrowableException
中,这是未选中的。
AccessException
is a checked exception, but it was thrown from the method that doesn't declare it in its throws
clause (actually - from the aspect intercepting that method). It's an abnormal condition in Java, so your exception is wrapped with UndeclaredThrowableException
, which is unchecked.
要获得你的异常,您可以在您的方面截获的方法的 throws
子句中声明它,或者使用另一个未经检查的异常(即 RuntimeException
)而不是 AccessException
。
To get your exception as is, you can either declare it in the throws
clause of the method being intercepted by your aspect, or use another unchecked exception (i.e. a subclass of RuntimeException
) instead of AccessException
.
这篇关于获取UndeclaredThrowableException而不是我自己的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!