gpt4 book ai didi

java - 获取 UndeclaredThrowableException 而不是我自己的异常

转载 作者:IT老高 更新时间:2023-10-28 20:41:18 25 4
gpt4 key购买 nike

我有以下代码

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

最佳答案

AccessException 是一个检查异常,但它是从未在其 throws 子句中声明它的方法抛出的(实际上 - 从拦截该方法的方面) .这是 Java 中的异常情况,因此您的异常被 UndeclaredThrowableException 包裹,这是未选中的。

要按原样获取异常,您可以在被切面拦截的方法的 throws 子句中声明它,或者使用另一个未经检查的异常(即 RuntimeException) 而不是 AccessException

关于java - 获取 UndeclaredThrowableException 而不是我自己的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5490139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com