Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

java - Getting UndeclaredThrowableException instead of my own exception

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...