获取 UndeclaredThrowableException 而不是我自己的异常
我有以下代码
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
。
I have the following code
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");
}
}
When I use a user that does not have permissions, I get java.lang.reflect.UndeclaredThrowableException
instead of AccessException
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AccessException 是一个已检查异常,但它是从未在其 throws 子句中声明它的方法中抛出的(实际上 - 从拦截该方法的方面来看)。这是 Java 中的异常情况,因此您的异常被 UndeclaredThrowableException 包裹起来,这是未经检查的。
要按原样获取异常,您可以在方面拦截的方法的
throws
子句中声明它,或者使用另一个未经检查的异常(即RuntimeException
的子类) ) 而不是AccessException
。AccessException
is a checked exception, but it was thrown from the method that doesn't declare it in itsthrows
clause (actually - from the aspect intercepting that method). It's an abnormal condition in Java, so your exception is wrapped withUndeclaredThrowableException
, which is unchecked.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 ofRuntimeException
) instead ofAccessException
.