vendredi 23 juillet 2021

break out of try catch and if else statements in Java

I have the code block as below. I need to stop after i throw the exceptions in the elseif or else statements and not return the final return statement after the try catch if it goes into the elseif or else statements.

 @Override
    public Mono<JwToken> login(Account account) throws Exception {
        Mono<JwToken> jwtoken = null;
        try {
            sessionCountL = Integer.valueOf(sessionCount.get().value());
            if (sessionCountL < 3 && failedAttempts <3){
                Map<String, String> auth = new ConcurrentHashMap<>();

                String secretHash = calculateSecretHash;
                auth.put("USERNAME", account.getEmail());
                auth.put("PASSWORD", account.getPassword());
                auth.put("SECRET_HASH", secretHash);

                jwtoken = initiateAuth(auth, AuthFlowType.USER_PASSWORD_AUTH)
                        .map(response -> JwToken.builder().build());
                incrementSessionCount(account);  //this should be executed if the login is successful, if its not i need to throw an exception

            }
            else if(sessionCountL>2){
                log.info("Active sessions limit reached. Logging out from all devices. Session Count{}",sessionCountL);
                logout(account.getEmail());
                sessionCountL = 0;
                log.info("sessionCount{} updated", sessionCountL);
               throw new Exception("Active sessions limit reached. Logged out of all sessions."); //after it throws the exception i want to break out of this XYZ method
            }
            else{
                log.info("failed attempts else statement:");
                logout(account.getEmail());
                sessionCountL = 0;
                log.info("logged out of all active sessions and sessionCount updated to {}", sessionCountL);
                lockAccount(account.getEmail());
                failedAttempts = 0;
                log.info("Account is locked. Failed attempts count is updated to {}", failedAttempts);
                throw new Exception("Too many failed login attempts. Account is locked");//after it throws the exception i want to break out of this XYZ method
            }
        }
        catch(Exception e) {
            failedAttempts = +1;
            log.info("Main try catch-InitiateAuth error. Failed attempts updated to:{}",failedAttempts);
            e.printStackTrace();
            throw new Exception("Error:"+ e.getMessage());
        }
        log.info("Final return statement JwToken:{}",jwtoken);
        return jwtoken;

    }

Aucun commentaire:

Enregistrer un commentaire