获取 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<中声明它的方法抛出的/code> 子句(实际上 - 从拦截该方法的方面来看).这是 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屋!