简体   繁体   English

跳出 Java 中的 try catch 和 if else 语句

[英]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.我需要在 elseif 或 else 语句中抛出异常后停止,并且如果它进入 elseif 或 else 语句,则在 try catch 之后不返回最终的 return 语句。

 @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();
       
        }
        log.info("Final return statement JwToken:{}",jwtoken);
        return jwtoken;

    }

将 return 语句移到 if 条件中 then

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

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