繁体   English   中英

从布尔方法捕获并重新抛出异常的确会返回false,但是不执行任何操作会导致该方法不返回

[英]Catching and rethrowing an exception from a boolean method does return false whereas not doing anything causes the method not to return

我有一个关于Java编程语言以及它如何处理返回布尔值的异常和方法的常规查询。

请不要尽管尽管下面的示例涉及Spring / Ldap / ActiveDirectory,但我的问题仅是关于javaexceptions的问题

public boolean doAuthenticate(String userAndDomain, String password) {
        UsernamePasswordAuthenticationToken userToken = new UsernamePasswordAuthenticationToken(replaceBackSlashWithAtSign(userAndDomain), password);
        try {
            Authentication authentication = adAuthenticationProvider.authenticate(userToken);
            return authentication.isAuthenticated();
        } catch (BadCredentialsException e) {
            log.error("Authentication failed - wrong username\\password", e);
            throw new BadCredentialsException("Authentication failed - wrong username\\password", e);
        } catch (AuthenticationException e) {
            log.error("Authentication failed - AuthenticationException", e);
            throw new AuthenticationException("Authentication failed - AuthenticationException", e) { };
        }
    }

如果authenticate方法重新抛出BadCredentialsExceptionAuthenticationException任何一个,则doAuthenticate方法将返回false

但是,如果由于某种原因adAuthenticationProvider.authenticate() 引发另一个运行时异常 ,则该方法不会返回false, 也不会返回 ...

我只是想知道为什么会这样...

编辑

 LdapAuthentifier authentifier = new LdapAuthentifierImpl();
 boolean didAuthenticate = authentifier.doAuthenticate(VALID_USER, INVALID_PASSWORD);

如果抛出了两个指定的异常之一,则didAuthenticateSystem.out.println 确实显示为false ,而另一个异常停止了程序的执行,并且从未达到System.out.println

编辑2

public static void main(String[] args) {
 LdapAuthentifier authentifier = new LdapAuthentifierImpl();
 boolean didAuthenticate = authentifier.doAuthenticate(VALID_USER, INVALID_PASSWORD);
}

我了解发生了什么事。 这是解释。

其实,我在日志中看到的唯一的例外是BadCredentialsException但这种异常不会抛出 adAuthenticationProvider.authenticate因此永远不会被下面的方法重新抛出

实际发生的情况是authentication.isAuthenticated()返回的只是false,而我将此布尔值传递给客户端代码。

为了清楚起见,我再次包含该方法:

 @Override
    public boolean doAuthenticate(String userAndDomain, String password) {
        UsernamePasswordAuthenticationToken userToken = new UsernamePasswordAuthenticationToken(replaceBackSlashWithAtSign(userAndDomain), password);
        try {
            Authentication authentication = adAuthenticationProvider.authenticate(userToken);
            return authentication.isAuthenticated();
        } catch (BadCredentialsException e) {
            log.error("Authentication failed - wrong username\\password", e);
            throw new BadCredentialsException("Authentication failed - wrong username\\password", e);
        } catch (AuthenticationException e) {
            log.error("Authentication failed - AuthenticationException", e);
            throw new AuthenticationException("Authentication failed - AuthenticationException", e) { };
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM