[英]break out of try catch and if else statements in Java
我有如下代碼塊。 我需要在 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.