如何在ifPresent内部处理异常? [英] How to handle an exception inside of ifPresent?

查看:501
本文介绍了如何在ifPresent内部处理异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在方法内部,需要一个条件来进行逻辑处理.我的IDE中显示未处理的异常警告消息.用try-catch包裹整个块不会使消息消失.

Inside a method, a condition is needed to proceed the logic. An unhandled exception warning message shows up in my IDE. Wrapping the whole block with try-catch doesn't let the message go away.

public void changePassword(String login, String currentClearTextPassword, String newPassword) {

    userRepository.findOneByLogin(login)
            .ifPresent(user -> {
                String currentEncryptedPassword = user.getUserSecret();
                String encryptedInputPassword = "";
                try {
                    encryptedInputPassword = authUtils.encrypt(currentClearTextPassword);
                } catch (Exception ex) {
                    System.err.println("Encryption exception: " + ex.getMessage());
                }
                if (!Objects.equals(encryptedInputPassword, currentEncryptedPassword)) {
                    throw new Exception("Invalid Password");  // <-- unhandled exception
                }
                String encryptedNewPassword = "";
                try {
                    encryptedNewPassword = authUtils.encrypt(newPassword);
                } catch (Exception ex) {
                    System.err.println("Encryption exception: " + ex.getMessage());
                }
                user.setUserSecret(encryptedNewPassword);
                userRepository.save(user);
                log.debug("Changed password for User: {}", user);
            });
}

如何处理此警告消息?

推荐答案

在流操作内部处理异常会增加一些开销,我想将操作分开,这样:

Treating Exception inside stream operation is a little overhead, I would like to separate the operation and makes it like so :

public void changePassword(String login, String currentClearTextPassword, String newPassword) throws Exception {
    //get the user in Optional
    Optional<User> check = userRepository.findOneByLogin(login);

    //if the user is present 'isPresent()'
    if(check.isPresent()){

        //get the user from the Optional and do your actions
        User user = check.get();

        String currentEncryptedPassword = user.getUserSecret();
        String encryptedInputPassword = "";
        try {
            encryptedInputPassword = authUtils.encrypt(currentClearTextPassword);
        } catch (Exception ex) {
            throw new Exception("Encryption exception: " + ex.getMessage());
        }
        if (!Objects.equals(encryptedInputPassword, currentEncryptedPassword)) {
            throw new Exception("Invalid Password");  // <-- unhandled exception
        }
        String encryptedNewPassword = "";
        try {
            encryptedNewPassword = authUtils.encrypt(newPassword);
        } catch (Exception ex) {
            throw new Exception("Encryption exception: " + ex.getMessage());
        }
        user.setUserSecret(encryptedNewPassword);
        userRepository.save(user);
        log.debug("Changed password for User: {}", user);
    }
}

除了抛出异常外,还应该抛出异常.

Beside the Exception should be thrown not just printed.

这篇关于如何在ifPresent内部处理异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆