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

StackOverflow https://stackoverflow.com/questions/23611436

  •  20-07-2023
  •  | 
  •  

Question

I have a general query regarding the java programming language and how it deals with exceptions and methods returning boolean.

Please not that although the example below deals with Spring/Ldap/ActiveDirectory, my question is only about java and exceptions.

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) { };
        }
    }

If any of BadCredentialsException or AuthenticationException is rethrown by the authenticate method, then the doAuthenticate method returns false.

However if for some reason another runtime exception is thrown by adAuthenticationProvider.authenticate(), then the method does not return false and does not return at all...

I am just curious to know why that is...

edit:

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

A System.out.println of didAuthenticate does show false if one of the two specified exceptions are thrown whereas another exception halts execution of the program and the System.out.println is never reached...

edit 2:

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

Solution

I understand what happened. Here is the explanation.

The exception I actually saw in the logs was BadCredentialsException but this exception is never thrown by adAuthenticationProvider.authenticate and therefore never rethrown by the below method.

What actually happened was that the authentication.isAuthenticated() was just returning false and I was passing this boolean value to the client code.

I am including the method again for clarity's sake:

 @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) { };
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top